Dave's Brain

Browse - Programming Tips - How can I turn a Windows error number into a string?

Date: 2014sep26
Platform: Win32
OS: Windows
Language: C/C++

Q.  How can I 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));

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.