سؤال

لدي هذا الرمز في مشروع 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() );
    }
}

لذلك يمكن للاستدعاء الفعلي التقاط الاستثناءات وترجمتها لتوفير تشخيصات ذات معنى.من أجل الحصول على التشخيص الذي تحتاج إلى استرداده IErrorInfo* بعد عودة الوظيفة.استخدم الكود من check() وظيفة من نفس المشروع المثال لذلك.فقط لا تحدق في الاستثناء الذي تم طرحه - ليس لديك أي فرصة لذلك، دعه ينتشر ويترجم.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top