COM: how to get more detailed information about COM errors?

hello

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

DXGetErrorString9

and

DXGetErrorDescription9

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

But now, working with COM and OLE, I believe that I'm on my own with HRESULTS, which return from COM functions. Is it really just me and MSDN at the moment, or are there similar helper functions in OLE DB that I haven't met yet?

+3
source share
2 answers

, . COM , , . , , , 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
  }
}
+3

_com_error :

#include <comdef.h>

HRESULT hr = SomeComFunc();
if ( FAILED(hr) )
{
  _com_error err(hr);
  LPTCSTR szErrMsg = err.ErrorMessage();
  // log szErrMsg or whatever 
}
+1

Source: https://habr.com/ru/post/1712421/


All Articles