加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何(有办法)将HRESULT转换为系统特定的错误消息?

发布时间:2020-12-16 03:15:01 所属栏目:百科 来源:网络整理
导读:根据 this,无法将HRESULT错误代码转换为Win32错误代码.因此(至少我的理解),我使用FormatMessage为了生成错误消息(即 std::wstring Exception::GetWideMessage() const{ using std::tr1::shared_ptr; shared_ptrvoid buff; LPWSTR buffPtr; DWORD bufferLengt
根据 this,无法将HRESULT错误代码转换为Win32错误代码.因此(至少我的理解),我使用FormatMessage为了生成错误消息(即
std::wstring Exception::GetWideMessage() const
{
    using std::tr1::shared_ptr;
    shared_ptr<void> buff;
    LPWSTR buffPtr;
    DWORD bufferLength = FormatMessageW(
        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetErrorCode(),reinterpret_cast<LPWSTR>(&buffPtr),NULL);
    buff.reset(buffPtr,LocalFreeHelper());
    return std::wstring(buffPtr,bufferLength);
}

)不适用于HRESULT.

如何为HRESULT生成这些类型的特定于系统的错误字符串?

解决方法

这个答案结合了Raymond Chen的想法,并正确识别了传入的HRESULT,并使用正确的设备返回一个错误字符串来获取错误消息:
/////////////////////////////
// ComException

CString FormatMessage(HRESULT result)
{
    CString strMessage;
    WORD facility = HRESULT_FACILITY(result);
    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
            strMessage.Append(bstr);

            // done with BSTR,do manual cleanup
            SysFreeString(bstr);
        }
    }
    else if (facility == FACILITY_ITF)
    {
        // interface specific - no standard mapping available
        strMessage.Append(_T("FACILITY_ITF - This error is interface specific.  No further information is available."));
    }
    else
    {
        // attempt to treat as a standard,system error,and ask FormatMessage to explain it
        CString error;
        CErrorMessage::FormatMessage(error,result); // <- This is just a wrapper for ::FormatMessage,left to reader as an exercise :)
        if (!error.IsEmpty())
            strMessage.Append(error);
    }
    return strMessage;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读