Skip to content

Instantly share code, notes, and snippets.

@JPGygax68
Last active September 18, 2017 19:07
Show Gist options
  • Save JPGygax68/5467121 to your computer and use it in GitHub Desktop.
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
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