Getlasterror ()

Source: Internet
Author: User
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 ()".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.