Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get a text from the error code returns from the GetLastError() function

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

like image 326
Tim Avatar asked Jun 09 '10 13:06

Tim


People also ask

What is the return value of GetLastError () function?

The GetLastError function returns the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.

What can GetLastError () function do explain?

An application can retrieve the last-error code by using the GetLastError function; the error code may tell more about what actually occurred to make the function fail. The documentation for system functions will indicate the conditions under which the function sets the last-error code.

How do I get error messages in C++?

The perror() function in C++ prints the error message to stderr based on the error code currently stored in the system variable errno.

How do you use last-error?

If your application needs more details about an error, it can retrieve the last-error code using the GetLastError function and display a description of the error using the FormatMessage function. The following example includes an error-handling function that prints the error message and terminates the process.


1 Answers

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

like image 66
humbagumba Avatar answered Oct 04 '22 21:10

humbagumba