views:

142

answers:

2

Hi, I need to get the text of an error code that i got from the GetLastError function. I saw a few examples but i want a function that get the code and return the string. Thank's you all

A: 

I forgot to notice... c++

Tim
remove your answer and edit your question for C++ (add C++ tag)
serhio
Added the tag for you, just delete this answer. Next time, just edit your question.
Felix Kling
+4  A: 

I guess you want something like this:

DWORD   dwLastError = ::GetLastError();
TCHAR   lpBuffer[256] = _T("?");
if(dwLastError != 0)    // Don't want to see a "operation done successfully" error ;-)
    ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,                 // It´s a system error
                     NULL,                                      // No string to be formatted needed
                     dwLastError,                               // Hey Windows: Please explain this error!
                     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),  // Do it in the standard language
                     lpBuffer,              // Put the message here
                     STR_ELEMS(lpBuffer)-1,                     // Number of bytes to store the message
                     NULL);

Also see: http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx

humbagumba
Thank's for helping me
Tim
Mind setting this to answered then? Thanks!
humbagumba
You *really* have to lose that static_cast, that just generates Chinese or one letter messages.
Hans Passant
Actually, it works just fine. I copied this from one of our projects. But don't ask me why someone put the cast in there... I removed it.
humbagumba