c++cwinapigetlasterrorformatmessage

Rectify this Error:GetLastError 0x13d


In this sample,the dwerror is 10045L.but this code returns 0x13d value as error. How to get the format message?Please have a look into it.

TCHAR lpMsgBuf[512];
if(!FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    dwError,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    (LPTSTR) &lpMsgBuf,
    0, NULL ))
{
    wprintf(L"Format message failed with 0x%x\n", GetLastError());
    return;
}

Solution

  • 0x13d == 317 == ERROR_MR_MID_NOT_FOUND. The message for the error you're trying to find doesn't exist in SYSTEM. Maybe your error originate from a specific dll or driver. If you know which dll\driver try to abtain it handle and specify FORMAT_MESSAGE_FROM_HMODULE instead of FORMAT_MESSAGE_FROM_SYSTEM and supply the handle as the source in the call to FormatMessage.

    And besides that if you use FORMAT_MESSAGE_ALLOCATE_BUFFER you should declare a variable of type LPTSTR like LPTSTR pMsg; and pass it to FormatMessage as (LPTSTR)&pMsg and when you're done with it use LocalFree(pMsg) to release the allocated memory.