Aar*_*ron 84 c++ windows error-handling formatmessage
没有:
如何使用FormatMessage()
获取错误文本HRESULT
?
HRESULT hresult = application.CreateInstance("Excel.Application");
if (FAILED(hresult))
{
// what should i put here to obtain a human-readable
// description of the error?
exit (hresult);
}
Run Code Online (Sandbox Code Playgroud)
Sho*_*og9 128
这是从系统中获取错误消息的正确方法HRESULT
(在这种情况下命名为hresult,或者您可以替换它GetLastError()
):
LPTSTR errorText = NULL;
FormatMessage(
// use system message tables to retrieve error text
FORMAT_MESSAGE_FROM_SYSTEM
// allocate buffer on local heap for error text
|FORMAT_MESSAGE_ALLOCATE_BUFFER
// Important! will fail otherwise, since we're not
// (and CANNOT) pass insertion parameters
|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM
hresult,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&errorText, // output
0, // minimum size for output buffer
NULL); // arguments - see note
if ( NULL != errorText )
{
// ... do something with the string `errorText` - log it, display it to the user, etc.
// release memory allocated by FormatMessage()
LocalFree(errorText);
errorText = NULL;
}
Run Code Online (Sandbox Code Playgroud)
这与David Hanak的答案之间的关键区别在于使用FORMAT_MESSAGE_IGNORE_INSERTS
旗帜.MSDN对如何使用插入有点不清楚,但是Raymond Chen指出在检索系统消息时不应该使用它们,因为你无法知道系统期望的插入.
FWIW,如果您使用的是Visual C++,那么使用_com_error
该类可以让您的生活更轻松:
{
_com_error error(hresult);
LPCTSTR errorText = error.ErrorMessage();
// do something with the error...
//automatic cleanup when error goes out of scope
}
Run Code Online (Sandbox Code Playgroud)
据我所知,直接不是MFC或ATL的一部分.
Mar*_*ius 14
请记住,您无法执行以下操作:
{
LPCTSTR errorText = _com_error(hresult).ErrorMessage();
// do something with the error...
//automatic cleanup when error goes out of scope
}
Run Code Online (Sandbox Code Playgroud)
在堆栈上创建和销毁类时,会将errorText指向无效位置.在大多数情况下,此位置仍将包含错误字符串,但在编写线程应用程序时,这种可能性很快就会消失.
所以总是按照上面Shog9的回答如下:
{
_com_error error(hresult);
LPCTSTR errorText = error.ErrorMessage();
// do something with the error...
//automatic cleanup when error goes out of scope
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*nak 12
试试这个:
void PrintLastError (const char *msg /* = "Error occurred" */) {
DWORD errCode = GetLastError();
char *err;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPTSTR) &err,
0,
NULL))
return;
static char buffer[1024];
_snprintf(buffer, sizeof(buffer), "ERROR: %s: %s\n", msg, err);
OutputDebugString(buffer); // or otherwise log it
LocalFree(err);
}
Run Code Online (Sandbox Code Playgroud)
从 c++11 开始,您可以使用标准库而不是FormatMessage
:
#include <system_error>
std::string message = std::system_category().message(hr)
Run Code Online (Sandbox Code Playgroud)
这更像是对大多数答案的补充,但不是LocalFree(errorText)
使用HeapFree
函数:
::HeapFree(::GetProcessHeap(), NULL, errorText);
Run Code Online (Sandbox Code Playgroud)
Windows 10:
LocalFree 不在现代 SDK 中,因此它不能用于释放结果缓冲区。相反,使用 HeapFree(GetProcessHeap()、locatedMessage)。在这种情况下,这与在内存上调用 LocalFree 相同。
更新
我发现它LocalFree
在 SDK 的 10.0.10240.0 版中(WinBase.h 中的第 1108 行)。但是,警告仍然存在于上面的链接中。
#pragma region Desktop Family or OneCore Family
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
WINBASEAPI
_Success_(return==0)
_Ret_maybenull_
HLOCAL
WINAPI
LocalFree(
_Frees_ptr_opt_ HLOCAL hMem
);
#endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) */
#pragma endregion
Run Code Online (Sandbox Code Playgroud)
更新 2
我还建议使用该FORMAT_MESSAGE_MAX_WIDTH_MASK
标志来整理系统消息中的换行符。
FORMAT_MESSAGE_MAX_WIDTH_MASK
该函数忽略消息定义文本中的常规换行符。该函数将消息定义文本中的硬编码换行符存储到输出缓冲区中。该函数不会产生新的换行符。
更新 3
似乎有 2 个特定的系统错误代码不会使用推荐的方法返回完整消息:
为什么 FormatMessage 只为 ERROR_SYSTEM_PROCESS_TERMINATED 和 ERROR_UNHANDLED_EXCEPTION 系统错误创建部分消息?