Programming Tips - Win32: Turn a Windows error number into a string

Date: 2014sep26 Update: 2025aug5 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); } void ExampleUse() { const DWORD dwError = GetLastError(); char szError[1024]; ErrorToString(dwError, szError, sizeof(szError)); printf("Error=%s\n", buf); }