2009-07-12 54 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

相關問題