Search code examples
c++comoledb

COM: how to get more details about COM errors?


Greets,

When working with DirectX, you get this nice header to #include called DxErr9.h which has really helpful functions like:

DXGetErrorString9

and

DXGetErrorDescription9

They tell you everything you need to know about the error given the HR.

But now working with COM and OLE, I find I'm kind of on my own with the HRESULTS that come back from COM functions. Is it really just me and MSDN at this point, or are there similar helper functions in OLE DB that I just haven't come across yet?


Solution

  • Additionally, you should look at the error info. Part of the COM system is the concept of the error information, which is a per-thread global which can be set and cleared at various times. You query for it in response to an error, and if it is set, it will have more useful information than just looking at the HRESULT.

    HRESULT hr=something();
    if (FAILED(hr))
    {
      CComPtr<IErrorInfo> err;
      ::GetErrorInfo(0, &err);
      if (err)
      {
        CComBSTR description;
        err->GetDescription(&description);
    
        // description will be a more descriptive error message than just formatting the 
        // HRESULT because it is set by the COM server code at the point of the error
      }
    }