取代 COM 錯誤處理所使用的預設函式。 _set_com_error_handler是Microsoft特定的。
語法
void __stdcall _set_com_error_handler(
void (__stdcall *pHandler)(
HRESULT hr,
IErrorInfo* perrinfo
)
);
參數
pHandler
取代函式的指標。
人力資源
HRESULT 資訊。
perrinfo
IErrorInfo 物件
備註
根據預設, _com_raise_error 會處理所有 COM 錯誤。 您可以使用_set_com_error_handler來呼叫自己的錯誤處理函式來變更此行為。
取代函式擁有的簽章必須相當於 _com_raise_error 的簽章。
範例
// _set_com_error_handler.cpp
// compile with /EHsc
#include <stdio.h>
#include <comdef.h>
#include <comutil.h>
// Importing ado dll to attempt to establish an ado connection.
// Not related to _set_com_error_handler
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
void __stdcall _My_com_raise_error(HRESULT hr, IErrorInfo* perrinfo)
{
throw "Unable to establish the connection!";
}
int main()
{
_set_com_error_handler(_My_com_raise_error);
_bstr_t bstrEmpty(L"");
_ConnectionPtr Connection = NULL;
try
{
Connection.CreateInstance(__uuidof(Connection));
Connection->Open(bstrEmpty, bstrEmpty, bstrEmpty, 0);
}
catch(char* errorMessage)
{
printf("Exception raised: %s\n", errorMessage);
}
return 0;
}
Exception raised: Unable to establish the connection!
需求
標頭:<comdef.h>
Lib: 如果 已指定 /Zc:wchar_t 編譯程式選項(預設值),請使用 comsuppw.lib 或 comsuppwd.lib。 如果已指定 /Zc:wchar_t- 編譯程序選項,請使用 comsupp.lib。 如需詳細資訊,包括如何在 IDE 中設定此選項,請參閱 /Zc:wchar_t (wchar_t 為原生類型)。