Click here to Skip to main content
15,907,231 members
Articles / Desktop Programming / MFC

Get Windows Win32 Error Text - Simplified

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
31 Jul 2013CPOL 19.8K   7   8
Getting the text of Win32 Error codes for 99% of cases.

This code gets Win32 error text. It is written to be read and understood on the first pass. Its arbitrary use of a static buffer is simply to not force one to remember to free the text it returns. It does sacrifice thread safety. It is also written to be a candidate for wrapping in __try/__except as well as try/catch exception handing. The use of objects such as CString would inhibit __try/__except vis-a-vis stack unwinding. Hopefully it's hard to break and easy to change. 

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <conio.h>

// In Visual Studio, in Project > Properties > General > Character Set: use 'Not Set' or 'Multi-Byte'
char *GetErrorText(DWORD dwCode)
    static char buff[2000]        // Longest is 403 bytes, but memory is cheap.
    char *pmsg;
    int nbytes = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM,		// formatting options flag
            NULL,                		// not a source DLL or format string
            dwCode,                		// returned by GetLastError()
            (LPTSTR)&pmsg,       	     	// var to recv alloc'd buffer address

    DWORD dw = GetLastError();
    if (dw==0)	// NO_ERROR, originally defined as ERROR_SUCCESS
    {        	// usually ERROR_INVALID_PARAMETER (87L) ' The parameter is incorrect.'
        LocalFree((HLOCAL)pmsg); // Free pmsg after copying to buff[]  
    return buff;
int main()
    for (int code=0; code<2000000; code++)
        if (strlen(msg))
            printf("Win32 Code %3d: %s", code, msg);  // Already has <CR><LF> on end
//            Sleep(20);    // uncomment to see it scroll past
    // Will keep the console up until you press a key.
    puts("\nPress any key to continue....");
    while (!_kbhit()) Sleep(10);    


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Comments and Discussions

GeneralMy vote of 5 Pin
yafan5-Aug-13 2:50
yafan5-Aug-13 2:50 
SuggestionA Few Details Pin
Rick York1-Aug-13 9:29
mveRick York1-Aug-13 9:29 
GeneralRe: A Few Details Pin
Thomas B Dignan1-Aug-13 11:10
Thomas B Dignan1-Aug-13 11:10 
SuggestionMy vote of 2 Pin
Klaus-Werner Konrad31-Jul-13 22:20
Klaus-Werner Konrad31-Jul-13 22:20 
- no use of TCHAR, instead pure char[] used --> not really Unicode-ready.
- no need for this copy operation (may be also ot Unicode-ready)

Simply OMIT the FORMAT_MESSAGE_ALLOCATE_BUFFER flag, and pass the buffer
adress directly in the lpBuffer parameter - done !
GeneralRe: My vote of 2 Pin
Thomas B Dignan1-Aug-13 4:43
Thomas B Dignan1-Aug-13 4:43 
GeneralRe: My vote of 2 Pin
Klaus-Werner Konrad1-Aug-13 8:22
Klaus-Werner Konrad1-Aug-13 8:22 
GeneralMy vote of 5 Pin
Michael Haephrati31-Jul-13 9:47
professionalMichael Haephrati31-Jul-13 9:47 
GeneralRe: My vote of 5 Pin
Richard Andrew x6431-Jul-13 11:13
professionalRichard Andrew x6431-Jul-13 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.