Sep 112011

OK, it’s my birthday so I’ll do a short one this time. This function must have been written a hundred times, but it is useful. When you have an error from many Win32 functions you typically get back a, not so useful, error number. This function will return a string for that error in a really convenient CString variable.

CString GetCriticalErrorString(DWORD nError)
{
    LPVOID lpMsgBuf;

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        nError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
    CString cs = (LPCTSTR)lpMsgBuf;
    LocalFree(lpMsgBuf);
    return cs;
}

Leave a Reply

(required)

(required)