VC + + GetLastError () instructions for use

Source: Internet
Author: User
Tags win32 error

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

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.