VC GetLastError () get the use of error information
When you write an application in VC, you often need to deal with error handling issues. Many function calls use only true and false to indicate the result of a function's operation. In the event of an error, it is often noted in MSDN that you use the GetLastError () function to obtain the cause of the error.
The problem is that GetLastError () returns only a double-byte value (DWORD). Oh,my god! The current WIN32 error number has been ranked from 0 to 11031, which is not all of the error codes. Because the error code is constantly increasing.
I don't think anyone wants to check the error message for the error code. Fortunately, windows (above Windows95, Window NT 3.1 or above) already provides a ready-made error-handling function: FormatMessage (). The following is an example of the error message that is returned by GetLastError () by using FormatMessage () to get the wrong encoding:
#include <stdio.h>
#include <windows.h>
int WINAPI WinMain
(
HINSTANCE HInstance,
HInstance hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
LPVOID lpMsgBuf;
FormatMessage (
format_message_allocate_buffer|
format_message_from_system|
Format_message_ignore_inserts,
Null
GetLastError (),
Makelangid (Lang_neutral,sublang_default),
(LPTSTR) &lpmsgbuf,
0,
Null
);
PROCESS any inserts in lpMsgBuf.
DISPLAY the STRING.
MessageBox (NULL, (LPCTSTR) lpmsgbuf, "Error", MB_OK | Mb_iconinformation);
Free the BUFF.
LocalFree (LPMSGBUF);
return 0;
}
〖0〗-operation completed successfully.
〖1〗-function error.
The 〖2〗-system cannot find the file specified.
The 〖3〗-system could not find the path specified.
The 〖4〗-system cannot open the file.
〖5〗-denied access.
The 〖6〗-handle is not valid.
The 〖7〗-Storage control block is corrupted.
〖8〗-There is not enough storage space to process this command.
〖9〗-Storage Control block address is invalid.
〖10〗-environment error.
VC + + GetLastError () instructions for use