使用 FormatMessage 格式化 Windows 错误码
2022-01-26 01:56 CST
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // !WIN32_LEAN_AND_MEAN
#include <Windows.h>
std::string str_win_err(int err)
{
LPSTR buf = nullptr;
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
nullptr,
err,
0,
(LPSTR)&buf,
0,
nullptr
);
std::string msg;
if (buf) {
msg.assign(buf);
LocalFree(buf);
}
return msg;
}
使用 FormatMessage 格式化 Windows 错误码 by mkckr0 is licensed under CC BY-NC-ND 4.0