Handle errors in Windows

Source: Internet
Author: User

When a win API function is used, it checks the validity of the parameter and then tries to execute the task. if an invalid parameter is passed or the program cannot be correctly executed for some reason, the operating system returns a value indicating that the function fails to run to some extent. windows functions have five return value types.

 

Void indicates that the function execution cannot fail, or you do not have to worry about the failure. windows functions are rarely of this type. while we usually write code, we should also try to write a function with a return value, unless you are sure it will not be unexpected in the middle.

 

If bool fails, it returns 0. If it succeeds, it is not 0. therefore, you can determine whether a function has been successfully executed by checking whether its return value is 0 or not. note: Do not write such code if (ret = true ). this is not safe because bool only has 0 and 1. it is in windef. defined in H, typedef int bool;

 

If the handle function fails, null is usually returned. Otherwise, the handle value is returned to identify an object that you can operate on. however, the return value of some functions is invalid_handle_value, which is defined as-1. the SDK documentation describes the meaning of each function's return value. be careful when using it. createfile is such a function.

 

The long/DWORD value is hard to process. It is usually a function that returns the number of results. if, for some reason, the function cannot count the expected value, then the function usually returns 0 or-1 (depending on the relevant function ). be especially careful when using it.

 

 

In addition, a set of error code lists are defined in windows, which are obtained by specifying the error information after the current API is executed. You can use the getlasterror function.

# Include <windows. h> <br/> # include <stdio. h> </P> <p> int main () <br/> {<br/> // open the file <br/> handle hfile = createfile ("file.txt ", 0, 0, null, open_existing, 0, null); </P> <p> dword er = getlasterror (); </P> <p> return 0; <br/>}

 

The returned value er stores the error code recorded by the system after calling createfile. You can see that the value of hfile is invalid_handle_value.

Note: Not every API function call will change this error code. Some functions will not be modified when execution is successful. Pay special attention to this.

 

 

You can enter @ err in the "monitoring" window in the VC environment, and the information window on the right will display the information corresponding to the current error code.

As shown in the preceding column, we set the breakpoint to return 0;. When it is executed, we follow the above method. You will find the following prompt appears in the invigilation window:

@ Err, HR 0x00000002 the system cannot find the specified file.

 

Of course, we can also output formatted strings through the error code. The following functions can do this.

DWORD formatmessage (

DWORD dwflags,

Lpvoid psource,

DWORD dwmessageid,

Dwrod dwageid,

Ptstr pszbuffer,

DWORD nsize;

Va_list * arguments );

 

This function is very powerful and can be operated in multiple languages.

 

# Include <windows. h> <br/> # include <stdio. h> </P> <p> int main () <br/> {<br/> // open the file <br/> handle hfile = createfile ("file.txt ", 0, 0, null, open_existing, 0, null); </P> <p> dword er = getlasterror (); <br/> hlocal local; <br/> char * buffer [256]; <br/> // formatmessage (format_message_allocate_buffer | format_message_from_system, null, <br/> // er, makelangid (lang_english, sublang_english_us ), (ptstr) & Local, 0, null); </P> <p> formatmessage (format_message_allocate_buffer | format_message_from_system, null, <br/> er, makelangid (lang_chinese, Hangzhou ), (ptstr) & Local, 0, null); <br/> printf ("% s", (pctstr) locallock (local); <br/> getchar (); <br/> return 0; <br/>}< br/>

 

The annotated part is output in English, and I have just said that this part supports multiple languages, so the following describes how to support Chinese. It should be noted that this function has many functions. I just demonstrated how to get error information from the system and local buffer.

 

# Define format_message_allocate_buffer 0x00000100
# Define format_message_ignore_inserts 0x00000200
# Define format_message_from_string 0x00000400
# Define format_message_from_hmodule 0x00000800
# Define format_message_from_system 0 0x00001000
# Define format_message_argument_array 0x00002000
# Define format_message_max_width_mask 0x000000ff

 

It can be seen from its powerful definition that many things are supported. For example, the following combination:

Format_message_from_hmodule | format_message_from_system can be used for network-related errors ..

 

 

Similarly, you can define your own error code. The error code is a 32-bit value.

31-30 characters indicates the severity of the error. 0 = Success 1 = for Reference 2 = warning 3 = Error

29-bit indicates the error provider. 0 indicates that the provider is provided by ms, and 1 indicates that the provider is provided by the user. Therefore, the 29-bit custom code must be 1.

28 BITs, reserved, must be 0.

27-16 bits, defined by MS, mark the device code.

15-0 digits indicates the Exception Code, which must be used by both ms and the customer.

 

 

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.