Skip to content

Instantly share code, notes, and snippets.

@crsib
Created May 5, 2022 09:54
Show Gist options
  • Save crsib/cb2785355f145e5e2dbd13e312c19b41 to your computer and use it in GitHub Desktop.
Save crsib/cb2785355f145e5e2dbd13e312c19b41 to your computer and use it in GitHub Desktop.
Last system error message
std::string GetSysErrorMessage(unsigned long errorCode)
{
// get error message from system
LPTSTR messageBuffer;
if (
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&messageBuffer, 0, NULL) == 0)
{
return {};
}
if (messageBuffer == nullptr)
return {};
std::string result = messageBuffer;
LocalFree(messageBuffer);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment