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?