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,并使用正确的设备返回一个错误字符串来获取错误消息://///////////////////////////// ComExceptionCString 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 @R_404_4329@ion 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;}总结
以上是内存溢出为你收集整理的c – 如何(有办法)将HRESULT转换为系统特定的错误消息?全部内容,希望文章能够帮你解决c – 如何(有办法)将HRESULT转换为系统特定的错误消息?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)