我有一个第三方COM组件及其VC++中的c ++接口.我在下面的调用中遇到崩溃,这会导致我的应用程序崩溃.如何从这个不属于我的应用程序的功能中优雅地恢复?
inline _RecordsetPtr IGLibMgr::GetLibInfo ( _bstr_t LibPath ) {
struct _Recordset * _result = 0;
HRESULT _hr = raw_GetLibInfo(LibPath, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _RecordsetPtr(_result, false);
}
Run Code Online (Sandbox Code Playgroud)
它在最后一行崩溃了.我不认为我可以修改此代码,因为它是第三方COM的东西.我真的有什么选择?我只是想给用户打开消息框并优雅地返回.
如果您还没有在代码中执行此操作,则需要来自调用方:
try
{ // setup your invoke for your object...
IGLibMgrPtr spMgr = ....
bstr_t bstrPath = ....
// invoke your call.
_RecordsetPtr spRS = spMgr->GetLibInfo(bstrPath);
... continue normal processing ...
}
catch(const _com_error& ce)
{
// handle your error here.
}
Run Code Online (Sandbox Code Playgroud)
这在多个层面上很重要.最明显的是,您的IGLibMgr成员不仅可以抛出异常,bstr_t分配等也可以.当使用#importCOM DLL中的代码时,如果使用来自MSVC的comutil库的生成的智能指针,请习惯这种格式.
注意:_com_error该类提供了几个成员来获取错误发生的原因,包括HRESULT,错误描述字符串等.它甚至可以提供IErrorInfo对错误返回对象创建的访问权限,如果它非常好,可以提供该级别的详细信息.