0
votes
CString ErrorMessageFromErrorCode(DWORD errorCode)
{
    void* pMsgBuf = NULL;

    ::FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        errorCode,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &pMsgBuf,
        0,
        NULL);

    CString result;

    if ( pMsgBuf )
    {
        result = (LPTSTR) pMsgBuf;
        LocalFree(pMsgBuf);
    }

    if ( result.Length() == 0 )
        result = _T("Unknown error");


    return result;
}

When called with parameter 2, this function returns "The system cannot find the file specified.". When called with 0xC1, it returns "Unknown error". Microsoft Error Lookup utility shows this for 0xC1: "%1 is not a valid Win32 application.". So, when error message contains placeholder, my function doesn't work. Is there generic way to get the same result as Error Lookup, without knowing anything about error code?

1
Parameter 2? You mean FORMAT_MESSAGE_FROM_SYSTEM? Looks like it was working and you had tried to open or delete a file that didn't exist previously. - AJG85

1 Answers

3
votes

Try adding FORMAT_MESSAGE_IGNORE_INSERTS to the flags.