【问题标题】:Generate diagnostic message for HRESULT codes?为 HRESULT 代码生成诊断消息?
【发布时间】:2009-12-03 22:48:48
【问题描述】:

我希望能够执行与 FormatMessage 等效的操作 - 生成用于调试甚至运行时构建的文本消息,可以报告一些常见的 HRESULT,甚至吐出诸如严重性是什么、它是什么设施等信息是,并且可能是错误代码的描述。

我找到了this simple function,但它太简单了,而且似乎大多会产生“未知错误”。但到目前为止,我还没有发现任何看起来更有希望的东西。

我可以执行以下操作:

CComPtr<IErrorInfo> iei;
if (S_OK == GetErrorInfo(0, &iei) && iei)
{
    // get the error description from the IErrorInfo 
    BSTR bstr = NULL;
    if (SUCCEEDED(iei->GetDescription(&bstr)))
    {
        // append the description to our label
        Append(bstr);

        // done with BSTR, do manual cleanup
        SysFreeString(bstr);
    }
}
else if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
{
    // append the description to our label
    Append(CErrorMessage(HRESULT_CODE(hr)).c_str());
}

但是,我想知道我是否完成了比 _com_error 更多的事情。

有没有人知道为 HRESULT 生成错误日志输出的合理充实的工具?

【问题讨论】:

    标签: c++ windows com


    【解决方案1】:

    如果您直接使用 WIN32,FormatMessage() 调用应该可以帮助您。

    【讨论】:

      【解决方案2】:

      如您所见,_com_error::ErrorMessage() 应该可以解决问题。

      如果您收到“未知错误”,那么您收到的 HRESULT 可能不为 Windows 所知。对于这些消息,请尝试转储 HRESULT 值和 figuring out if they actually map to win32 error codes

      some com macros available可以帮你拆分HRESULT的位。

      【讨论】:

      • 这和 boost::system 库看起来都很好。谢谢。 :)
      【解决方案3】:

      您在使用 Boost 吗? boost::system 库会自动查找 HRESULT 和 Win32 API 结果代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多