COM:如何获取有关 COM 错误的更多详细信息?

发布于 2024-07-27 05:37:12 字数 296 浏览 0 评论 0原文

您好,

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

DXGetErrorString9

它们

DXGetErrorDescription9

会告诉您有关给定 HR 的错误的所有信息。

但现在使用 COM 和 OLE,我发现我只能自己处理从 COM 函数返回的 HRESULTS。 现在真的只有我和 MSDN 吗?或者 OLE DB 中是否有类似的辅助函数,但我还没有遇到过?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2024-08-03 05:37:12

此外,您还应该查看错误信息。 COM系统的一部分是错误信息的概念,它是一个per-线程全局,可以在不同时间设置和清除。 您查询它以响应错误,如果是设置,它将比仅仅查看 <代码>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
  }
}

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
  }
}
错爱 2024-08-03 05:37:12

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

#include <comdef.h>

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

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 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文