Windows function error handling

Source: Internet
Author: User

When calling a Windows function, it first checks the validity of each parameter passed to it, and then tries to execute the task. if an invalid parameter is passed or the operation cannot be performed for some reason, the operating system returns a value indicating that the function fails to run to some extent.
Common types of return values for Windows functions:
Void
Bool: 0, non-0, true, false
Handle: handle, null, invalid_handle_valid)
Pvoid: NULL, pvoid
Long/DWORD: depends on the specific function
...
Microsoft compiled a list of all possible error codes and assigned a 32-bit number for each error code. winerror. the H header file (more than 20 thousand lines) contains the micorsoft-defined error code list.
When a Windows function detects an error, it uses the thread-local storage mechanism to associate the error code number with the called thread. this enables threads to run independently of each other without affecting their respective error codes.
When a function returns an error, its return value indicates whether an error has occurred. To determine the error, you must call the DWORD getlasterror () function, it returns the 32-bit error code of the thread. visual Studio has an error lookup program that can replace the error code number with the corresponding text description. windows also provides the formatmessage function to convert the error code into its text description.
DWORD formatmessage (
DWORD dwflags, // source and processing options
Lpvoid lpsource, // message source
DWORD dwmessageid, // message identifier
DWORD dwlanguageid, // language identifier
Lptstr lpbuffer, // Message Buffer
DWORD nsize, // maximum size of Message Buffer
Va_list * arguments // array of message Inserts
);
We can also use this mechanism in our own functions. You only need to use the void setlasterror (DWORD dwerrcode) function, if winerror. if any code in H does not correctly reflect the nature of the error, you can create your own code. Partition Table of error code fields:
Bit 31 ~ 30 29 28 27 ~ 16 15 ~ 0
Content severity Microsoft/customer retention device code Exception Code

Meaning 0 = Success 0 = Code defined by Ms must follow 0 defined by Ms or customer
1 = reference
2 = warning 1 = Customer-defined code
3 = Error

Note: If you create your own error code, the 29-bit value must be 1.

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.