Microsoft Specific

Replaces the default function that is used for COM error-handling.

void __stdcall _set_com_error_handler(
   void (__stdcall *pHandler)(
      HRESULT hr, 
      IErrorInfo* perrinfo


  • pHandler
    Pointer to the replacement function.

  • hr
    HRESULT information.

  • perrinfo
    IErrorInfo object.


By default, _com_raise_error handles all COM errors. You can change this behavior by using _set_com_error_handler to call your own error-handling function.

The replacement function must have a signature that is equivalent to that of _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()
   _bstr_t bstrEmpty(L"");
   _ConnectionPtr Connection = NULL;
      Connection->Open(bstrEmpty, bstrEmpty, bstrEmpty, 0); 
   catch(char* errorMessage)
      printf("Exception raised: %s\n", errorMessage);

   return 0;

Exception raised: Unable to establish the connection!


Header: comdef.h

Lib: If the “wchar_t is Native Type” compiler option is on, use comsuppw.lib or comsuppwd.lib. If “wchar_t is Native Type” is off, use comsupp.lib. For more information, see /Zc:wchar_t (wchar_t Is Native Type).

See Also


Compiler COM Global Functions