How to get the error message from the error code returned by GetLastError()? How to get the error message from the error code returned by GetLastError()? c c

How to get the error message from the error code returned by GetLastError()?


//Returns the last Win32 error, in string format. Returns an empty string if there is no error.std::string GetLastErrorAsString(){    //Get the error message ID, if any.    DWORD errorMessageID = ::GetLastError();    if(errorMessageID == 0) {        return std::string(); //No error message has been recorded    }        LPSTR messageBuffer = nullptr;    //Ask Win32 to give us the string version of that message ID.    //The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,                                 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);        //Copy the error message into a std::string.    std::string message(messageBuffer, size);        //Free the Win32's string's buffer.    LocalFree(messageBuffer);                return message;}


Updated (11/2017) to take into consideration some comments.

Easy example:

wchar_t buf[256];FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,               NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),                buf, (sizeof(buf) / sizeof(wchar_t)), NULL);


MSDN has some sample code that demonstrates how to use FormatMessage() and GetLastError() together: Retrieving the Last-Error Code