Date: 2014sep26
Platform: Win32
OS: Windows
Language: C/C++
Q. Win32: turn a Windows error number into a string
A. Error numbers from GetLastError() can be converted into strings
with this function.
void ErrorToString(const DWORD dwError, LPSTR szOut, const size_t size)
{
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // The user default language
szOut,
(DWORD)size,
NULL);
}
// Example use
DWORD dwError = GetLastError();
char buf[1024];
ErrorToString(dwError, buf, sizeof(buf));