Last active
September 18, 2017 19:07
-
-
Save JPGygax68/5467121 to your computer and use it in GitHub Desktop.
Retrieve the text associated with a #Windows #error code, and return it as a std::string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static const std::string | |
getWindowsErrorString(int err = 0) | |
{ | |
LPSTR s; | |
if (err == 0) err = GetLastError(); | |
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
NULL, | |
err, | |
0, | |
(LPSTR)&s, | |
0, | |
NULL) != 0) | |
{ | |
LPSTR p = strchr(s, '\r'); | |
if (p) *p = '\0'; | |
std::string res = s; | |
LocalFree(s); | |
return res; | |
} | |
else { | |
return std::string("(Failed to retrieve error string"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment