How to know the error message returned by getlasterror ()
Www.firnow.com time: 2010-06-30 Author: Network Editor: mr. Abu CLICK: 1 [comment]
-
-
How does the comprehensive resource e-book community know the error message returned by getlasterror ()?
You can find the symbol definition of the error code in winerror. h to understand the general meaning of the error. You can also use formatmessage to obtain the string corresponding to the error.
When compiling applications in VC, it is often necessary to handle errors. Many function calls only use True or false to indicate the function running result. Once an error occurs, msdn often points out that you should use the getlasterror () function to obtain the cause of the error.
The problem is that getlasterror () returns only a dubyte value (DWORD ). Oh, my God! Currently, the error numbers of Win32 have been ranked from 0 to 11031, which is not all of the error codes. Because the error code is constantly increasing.
I think no one is willing to check the error message corresponding to the error code. Fortunately, Windows (Windows 95 and later, Windows NT 3.1 and later) already provides the ready-made error message processing function: formatmessage (). The following is an example of an error code returned by getlasterror () using formatmessage:
Lpvoid lpmsgbuf;
Formatmessage (
Format_message_allocate_buffer |
Format_message_from_system |
Format_message_ignore_inserts,
Null,
Getlasterror (),
Makelangid (lang_neutral, sublang_default), // default language
(Lptstr) & lpmsgbuf,
0,
Null
);
// Process any inserts in lpmsgbuf.
//...
// Display the string.
MessageBox (null, (lpctstr) lpmsgbuf, "error", mb_ OK | mb_iconinformation );
// Free the buffer.
Localfree (lpmsgbuf );
However, it is a bit ridiculous. "if an error occurs in this function call, the value 0 will be returned. You need to obtain the error message. Please call getlasterror ()".
Article Source: feino Network (www.firnow.com): http://dev.firnow.com/course/3_program/vc/vcxl/20100630/265391.html