tags:

views:

117

answers:

2

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?

+1  A: 

Use _com_error to get a meaningful string:

#include <comdef.h>

HRESULT hr = SomeComFunc();
if ( FAILED(hr) )
{
  _com_error err(hr);
  LPTCSTR szErrMsg = err.ErrorMessage();
  // log szErrMsg or whatever 
}
eran
Hmm, this leaves me with "Unspecified error". Though it is helpful I wonder why it can't give more information
bobobobo
+3  A: 

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
  }
}
1800 INFORMATION
Thank you. This gives "[Microsoft][ODBC Driver Manager] Data source name not found" .. lovely
bobobobo