据此,没有办法将HRESULT错误代码转换为Win32错误代码。 因此(至less对我的理解),我使用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生成这些types的系统特定的错误string?
PHP使用参数参数在服务器上运行可执行程
Process.Start(“IIS Manager.lnk”)失败,“系统找不到指定的文件”
C#System.Windows.Forms.TreeView:停止双击自动展开/折叠(并执行另一个处理程序)
在Windows上使用Python 3.x和Python 2.x一起使用pip版本
在Windows上的Pythonsubprocess输出?
编译依赖于ActiveX的C#WinForm,而不注册它
控制台应用程序使用比应该更多的线程
奇怪的用户代理开关
Parallel.For在Python中
这个答案包含了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 @R_932_4045@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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。