2009-07-12 76 views
3

映入眼帘,COM:如何获得有关COM错误的更多详细信息?

当与DirectX工作,你会得到这个漂亮的头的#include称为DxErr9.h具有真正有用的功能,如:

 
DXGetErrorString9 

 
DXGetErrorDescription9 

他们会告诉你所有你需要知道的人力资源错误。

但是现在使用COM和OLE,我发现我对自己的COM功能返回的HRESULTS有点独立。在这一点上,它真的只是我和MSDN吗?还是在OLE DB中还有类似的辅助函数,我还没有遇到过呢?

回答

3

此外,你应该看看error info。 COM系统的一部分是错误信息的概念,在不同的时间是per-thread global which can be set and cleared。您为query for it回应一个错误,如果它是set,它会比看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 
    } 
} 
+0

谢谢。这给“[Microsoft] [ODBC驱动程序管理器]数据源名称找不到”..可爱 – bobobobo 2009-07-12 20:46:48

1

使用_com_error得到一个有意义的字符串:

#include <comdef.h> 

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

嗯,这给我留下了“未指定的错误”。虽然这很有帮助,但我想知道为什么它不能提供更多信息 – bobobobo 2009-07-12 20:46:12

相关问题