Microsoft 固有の仕様 →
COM のエラー処理に使用する既定の関数を置き換えます。
void __stdcall _set_com_error_handler(
void (__stdcall *pHandler)(
HRESULT hr,
IErrorInfo* perrinfo
)
);
パラメーター
pHandler
置換関数へのポインター。hr
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;
}
必要条件
ヘッダー: comdef.h
「wchar_t ネイティブ型」のコンパイラ オプション**:解放** 場合は、使用 comsuppw.lib または comsuppwd.lib です。wchar_t 「型」はネイティブの場合、comsupp.lib をオフです。詳細については、「/Zc:wchar_t (wchar_t をネイティブ型として認識)」を参照してください。