我在C++/CLI项目中有这个代码:

CSafePtr<IEngine> engine;
HMODULE libraryHandle;

libraryHandle = LoadLibraryEx("FREngine.dll", 0, LOAD_WITH_ALTERED_SEARCH_PATH);

typedef HRESULT (STDAPICALLTYPE* GetEngineObjectFunc)(BSTR, BSTR, BSTR, IEngine**);
GetEngineObjectFunc pGetEngineObject = (GetEngineObjectFunc)GetProcAddress(libraryHandle, "GetEngineObject");

pGetEngineObject( freDeveloperSN, 0, 0, &engine )

最后一行抛出此异常:

RPC服务器不可用

什么可能导致此异常?

有帮助吗?

解决方案

ABBYY FRE是一个COM对象。 GetEngineObject() 行为像一个普通的COM接口方法,除了它是一个单独的函数。这意味着以下内容:它不允许异常传播到外部。为了实现这一点,它捕获所有异常,将它们转换为适当的 HRESULT 值和可能设置 IErrorInfo.

您试图分析方法内部抛出的异常,没有机会找到问题所在。那是因为在内部它可能像这样工作:

HRESULT GetEngineObject( params )
{
    try {
       //that's for illustartion, code could be more comlex
       initializeProtection( params );
       obtainEngineObject( params );
    } catch( std::exception& e ) {
       setErrorInfo( e ); //this will set up IErrorInfo
       return translateException( e ); // this will produce a relevant HRESULT
    }
    return S_OK;
}

void intializeProtection()
{
    try {
       doInitializeProtection();//maybe deep inside that exception is thrown
       ///blahblahblah
    } catch( std::exception& e ) {
       //here it will be translated to a more meaningful one
       throw someOtherException( "Can't initialize protection: " + e.what() );
    }
}

因此,实际调用可以捕获异常并转换它们以提供有意义的诊断。为了获得tha诊断,您需要检索 IErrorInfo* 后的功能retuns。使用代码从 check() 函数来自同一示例项目。只是不要盯着抛出的异常-你没有机会,让它传播和翻译。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top