Reputation: 67254
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?
Upvotes: 3
Views: 403
Reputation: 135295
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
}
}
Upvotes: 3
Reputation: 22020
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
}
Upvotes: 1