Windows core programming-function Return Error

Source: Internet
Author: User
1. Windows program error handling mechanism

Windows API return type
Data Type

Indicates the value of failure.

V o I d

The operation of this function cannot fail. The Return Value Type of the WI n d o w s function is rarely v o I D.

B o l

If the function fails to run, the returned value is 0. Otherwise, the returned value is not 0. It is best to test the returned value to determine whether it is 0 or not. Do not test whether the returned value is t r u e

H A n d l e

If the function fails to run, the return value is generally n u L. Otherwise, the return value is H a n d l e, which is used to identify an object that you can operate on. Note that some functions return a handle value I n valid _ handle_va L U E, which is defined as-1. The Platform SDK documentation of the function will clearly show whether n u l or I n va l I d _ H a n d l e _ va l I D is returned when the function fails to run.

P v o I d

If the function fails to run, the return value is n u L; otherwise, p v o I D is returned to identify the memory address of the data block.

L o n g/d w o r d

This is an unhandled value. The return number function usually returns l o n g or d w o r d. If, for some reason, the function cannot count the objects to be counted, the function usually returns 0 or-1 (depending on the function ). If the called function returns l o n g/d w o r d, read the Platform SDK documentation carefully to ensure that potential errors are correctly checked.

The WI n e r o r. h header file contains a list of error codes defined by m I c r o s o f t. Each error has three representations, for example:

// Messageid: error_path_not_found
// Messagetext:
// The system cannot find the path specified.
# Define error_path_not_found 3l

When an error occurs, you can use the following function to obtain the error ID,
DWORD getlasterror ();
You can also use the following function to obtain the corresponding text by ID:
DWORD formatmessage (
DWORD dwflags, // source and processing options
Lpvoid lpsource, // pointer to message source
DWORD dwmessageid, // requested message identifier
DWORD dwlanguageid, // language identifier for requested message
Lptstr lpbuffer, // pointer to message buffer
DWORD nsize, // maximum size of Message Buffer
Va_list * arguments // pointer to array of message Inserts
);
When an error occurs when an API is called, you can view the error using the following method immediately, but the subsequent API calls will overwrite the preceding errors.

Figure 1-1 in the wa t c h window of Visual Studio 6.0, type "@ e r, h r" to view the final error code of the current thread.

Visual Studio also has a small utility called error lookup. You can use error lookup to convert the error code number into the corresponding text description (see Figure 1-2 ).

You can also define your own error code, but you need to create a correct 32-bit ID. It cannot conflict with the system.

Table 1-2 error code fields

Bit 3 1 ~ 30 29 28 27 ~ 16 15 ~ 0
Content Severity M I c r o s o f t/customer Retained Device code Exception Code
Description 0 = successful 0 = m I c r o s o f t company-defined code Must be 0 Defined by m I c r o s o f t Defined by Microsoft/customer
  1 = for Reference 1 = Customer-defined code      
  2 = warning        
  3 = Error    

 
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.