//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
#ifdef UNICODE
std::wstring GetSysError(DWORD errCode)
#else
std::string GetSysError(DWORD errCode)
#endif
{
DWORD errorMessageID = errCode;
TCHAR* messageBuffer = nullptr;
size_t size = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorMessageID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(TCHAR *)&messageBuffer,
0,
NULL);
#ifdef UNICODE
std::wstring message(messageBuffer, size);
#else
std::string message(messageBuffer, size);
#endif
//Free the buffer.
LocalFree(messageBuffer);
return message;
转载于:https://my.oschina.net/3cwYg4/blog/2987983
获取Win32错误代码详解
本文介绍了一个用于获取Windows系统中最后发生的Win32错误代码的函数实现。该函数能够根据错误代码返回对应的错误信息,有助于开发者在遇到系统错误时进行诊断。文章提供了源代码示例,展示了如何使用FormatMessage函数将错误代码转换为人类可读的字符串格式。
503

被折叠的 条评论
为什么被折叠?



