【问题标题】:COM: how to get more details about COM errors?COM:如何获得有关 COM 错误的更多详细信息?
【发布时间】:2009-07-13 04:15:54
【问题描述】:

你好,

使用 DirectX 时,您会在 #include 中获得一个名为 DxErr9.h 的漂亮标头,它具有非常有用的功能,例如:

DXGetErrorString9

DXGetErrorDescription9

他们会告诉你所有你需要知道的关于 HR 给出的错误的信息。

但现在使用 COM 和 OLE,我发现我有点靠自己处理从 COM 函数返回的 HRESULTS。在这一点上真的只有我和 MSDN,还是在 OLE DB 中有我还没有遇到过的类似帮助函数?

【问题讨论】:

    标签: c++ com oledb


    【解决方案1】:

    此外,您应该查看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
      }
    }
    

    【讨论】:

    • 谢谢。这给出了“[Microsoft][ODBC Driver Manager] Data source name not found”.. 可爱
    【解决方案2】:

    使用 _com_error 获取有意义的字符串:

    #include <comdef.h>
    
    HRESULT hr = SomeComFunc();
    if ( FAILED(hr) )
    {
      _com_error err(hr);
      LPTCSTR szErrMsg = err.ErrorMessage();
      // log szErrMsg or whatever 
    }
    

    【讨论】:

    • 嗯,这给我留下了“未指定的错误”。虽然它很有帮助,但我想知道为什么它不能提供更多信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多