Windows API Error Codes

Source: Internet
Author: User

In most cases, the Windows API rarely throws an exception when an error occurs, most of which is handled by a function return value. (There are few functions in the Windows API that have no return value.) )

Windows API error handling usually follows: first the API function returns a special value indicating that an error has occurred inside the function, and the caller can then use GetLastError to obtain the corresponding error code.

Typically, the Windows API can be divided into the following categories according to the return type:

    • The return value is type bool. An error occurred with a return value of 0, otherwise a value other than 0 is returned.
    • The return value is of type handle. When an error occurs, NULL or INVALID_HANDLE_VALUE is returned (the value is-1).
    • The return value is a long type or a DWORD type. When an error occurs, return 0 or-1.

Because the Windows API return types are not quite consistent, it is recommended that you review the instructions on MSDN for the actual processing of Windows API errors and follow the explanations in them to handle the corresponding error codes.

DWORD WINAPI GetLastError (void);

The error code returned by calling the GetLastError function is a DWORD type (32bit), and its fixed-bit domain mapping format is as follows (under Windows Small-endian order, from low to high-numbered 0, 1, ..., 30,31; Note the following table is represented by the binary data):

-bit Meaning
Bit30~31 Security level, 00 = Security, 01 = information, 10 = warning, 11 = error
Bit29 Source of error, 0-microsoft definition, 1-user-defined error code
Bit28 Reserved bit, must be 0
Bit16~27

Source of error tool code, Microsoft defined (WINERROR.H)

Bit0~15

The tool corresponds to the status code, Microsoft or user defined.

If necessary, we can also define the error code, and use the Windows API similar error mechanism, specifically refer to the setlasterror , Setlasterrorex functions, But be aware that custom error codes do not repeat with Windows error codes.

Viewing the information for the error code can use the Error Lookup tool provided by Visual C + +, or you can enter "@err, hr" directly in the debugger's observation window.

Of course, you can also use the formatmessage function to convert the error code directly into the corresponding string. The calling code is as follows:

//Windows Error Code resolver, Errcodeparsedemo//A brief description of how to convert the error code to the corresponding string information//we recommend that you compile Unicode encoding with VS2005 or later#include <windows.h>#include<iostream>#include<WinError.h>usingstd::wcout;usingStd::endl;voidoutputformatmessage (DWORD errcode) {LPTSTR lpmsgbuf=NULL; FormatMessage (Format_message_allocate_buffer|Format_message_from_system|format_message_ignore_inserts, NULL, Errcode, Makelangid (Lang_neutral, Sublang_default), (LPTSTR)&lpMsgBuf,0, NULL); Wcout<<"ERR:"<< Std::hex << Errcode <<"MSG Tips:"<<Endl<< lpMsgBuf <<Endl; LocalFree (LPMSGBUF);}int_tmain (intARGC, _tchar*argv[]) {    //Output ChineseStd::wcout.imbue (Std::locale ("CHS")); //Setting the console title barSetconsoletitle (TEXT ("Errcodeparsedemo"));    Outputformatmessage (error_invalid_function);    Outputformatmessage (error_handle_eof); return 0; }
View Code

The above code can be downloaded from [email protected], the link is as follows: Https://git.oschina.net/Tocy/SampleCode.git. Located in the ErrCodeParseDemo.cpp.

Windows API Error Codes

Related Article

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.