共用方式為


_set_com_error_handler

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

LIB: ,如果「wchar_t 是原生型別」編譯器選項開啟,請使用 comsuppw.lib 或 comsuppwd.lib。 如果「wchar_t 是原生型別」關閉,使用 comsupp.lib。 如需詳細資訊,請參閱 /Zc:wchar_t (wchar_t 是原生型別)

請參閱

參考

編譯器 COM 全域函式