Windows core programming _ error handling

Source: Internet
Author: User

 

Article content
1. define your own error code
2. errorshow sample program
Before discussing many features provided by windows, we should first understand how various Windows functions handle errors.
When a Windows function is called, it verifies the parameters that we pass to it before executing the task. If the input parameter is invalid or the operation cannot be executed due to other reasons, the return value of the function will fail because of some reasons. The following table shows the data types of the return values used by most Windows functions.

Data Type indicates the value of function call failure
-------------------------------------
The void function cannot fail. Only a few windows functions return void values.
-------------------------------------
If the function fails, the return value is 0. Otherwise, the return value is a non-0 value. Test return should be avoided
Whether the return value is true. The safest way is to check whether it is not false.
-------------------------------------
If the handle function fails, the return value is usually null. Otherwise, the handle identifies an operation that can be performed.
. Note the returned value because some functions return invalid_handle_value.
Is defined as-1. The Platform SDK documentation clearly shows that the function is
Return null or invalid_handle_value to identify failure
-------------------------------------
If function call fails, the return value is null. Otherwise, pvoid identifies
Memory Address
-------------------------------------
Long/DWORD is tricky. The function that returns the Count usually returns long or DWORD. If
For some reason, the number cannot count what we want to be technical. It usually returns 0
Or-1.
-------------------------------------
Microsoft edited a list, listing all possible error codes, and assigned a 32-bit number for each error code.
If an error occurs when the function returns, you need to check the specific error. The getlasterror function is used for sentiment, as shown below:
DWORD getlasterror ();
This function can return a 32-bit error code with a 32-bit error code, which needs to be converted into more useful information. Windows provides a function to convert the error code into a text description. This function is a formatmessage and the declaration is as follows:
DWORD formatmessage (
DWORD dwflags,
Lpcvoid lpsource,
DWORD dwmessageid,
DWORD dwageid,
Lpstr lpbuffer,
DWORD nsize,
Va_list * arguments
);
Customize your own error code
We will try to use the existing code in winerror. h as long as the code can reflect the error we want to report. If any code in winerror. h cannot accurately reflect an error, you can create your own code. The error code is a 32-digit
31-30 29 28 27-16 15-0
-----------------------------------------------------------------------
Content severity Microsoft/customer retention Facility Code Exception Code
----------------------------------------------------------------------
Meaning 0 = Success 0 = Microsoft must be the first 256 values from Microsoft/
1 = Information (prompt) defined code Microsoft retains the customer's code
2 = warning 1 = Customer-defined
3 = Error Code

Errorshow sample program

DWORD dwerror;
// Get the error code
Dwerror = getdlgitemint (idc_edit_errorcode, null, false );
Hlocal = NULL;
Bool Fok = formatmessage (
Format_message_from_system | format_message_ignore_inserts |
Format_message_allocate_buffer,
Null, dwerror, 0,
(Ptstr) & hlocal, 0, null );
If (FOK & (hlocal! = NULL ))
{
Setdlgitemtext (idc_edit_errortext, (pctstr) locallock (hlocal ));
Localfree (hlocal );
}
Else
{
Setdlgitemtext (idc_edit_errortext, text ("no text found for this error number ."));
}

 

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.