Detailed description of HRESULT

Source: Internet
Author: User

1. Succeeded

The prototype is as follows:

BOOL succeeded (  HRESULT hr);

corresponding definition (Specific implementation): #define SUCCEEDED (HR) ((HRESULT) (HR)) >= 0)

2. FAILED

The prototype is as follows:

BOOL FAILED (  HRESULT hr);

corresponding definition (concrete implementation): #define FAILED (HR) ((HRESULT) (HR)) < 0)

Note:

Use these two macros to judge the return value HRESULT of a COM component;

Because the HRESULT value is a return state, there may be many states, so you cannot directly judge

3 about HRESULT

If the function executes normally, it returns S_OK, and the actual function run result is returned by the parameter pointer. If an exception is encountered, the COM system is judged to return the corresponding error value.

HRESULT value Meaning
S_OK 0x00000000 Success
S_FALSE 0x00000001 function completed successfully, but error occurred on return
E_INVALIDARG 0x80070057 parameter has error
E_OUTOFMEMORY 0x8007000E Memory Request Error
E_unexpected 0x8000FFFF Unknown exception
E_NOTIMPL 0x80004001 features not implemented
E_fail 0x80004005 no detailed error. General need to get Rich error error message (Note 1)
E_pointer 0x80004003 Invalid pointer
E_handle 0x80070006 Invalid Handle
E_abort 0x80004004 Terminate operation
E_accessdenied 0x80070005 Access Denied
E_nointerface 0x80004002 does not support interfaces

HRESULT is actually a double-byte value whose highest bit (bit) if 0 indicates success and 1 indicates an error. See the "Structure of COM Error Codes" description on MSDN. If we need to judge the return value in the program, we can use the comparison operation symbol; the switch switch statement; You can also use the macros provided by the VC:

HRESULT hr = call component function;
if (SUCCEEDED (HR)) {...}//If successful
......
if (FAILED (hr)) {...}//if failed
......

Most COM functions and the return value types of some interface member functions are HRESULT types. The return value of the HRESULT type reflects some of the conditions in the function, and its type definition specification is as follows:

31 30 29 28 16 15 0
|-----|--|------------------------|-----------------------------------|

Category code (30-31) reflects the result of the function call:
00 Call succeeded
01 contains some information
10 warnings
11 Error
The custom marker (29) reflects whether the result is a custom identity, 1 is yes, and 0 is not;
The opcode (16-28) identifies the source of the result operation, which is defined on the Windows platform as follows:
#define FACILITY_WINDOWS 8
#define Facility_storage 3
#define FACILITY_RPC 1
#define FACILITY_SSPI 9
#define FACILITY_WIN32 7
#define Facility_control 10
#define FACILITY_NULL 0
#define Facility_internet 12
#define FACILITY_ITF 4
#define Facility_dispatch 2
#define Facility_cert 11
The operation result code (0-15) reflects the state of the operation, and WinError.h defines all possible return results for the WIN32 function.
Here are some commonly used return values and macro definitions:
The S_OK function executes successfully with a value of 0 (note that its value is the opposite of TRUE)
The S_FALSE function executes successfully with a value of 1
S_fail function execution failed, failure reason indeterminate
E_outofmemory function failed because the memory allocation was unsuccessful
E_NotImpl function execution failed, member function not implemented
E_notinterface function execution failed, component does not implement specified interface

Note: It is not easy to compare the return value to S_OK and S_FALSE, but to use secceeded and failed macros to judge

Failed and succeeded are macro functions defined in Windows, and S_OK is just a specific value.

#define SUCCEEDED (status) (HRESULT) (status) >= 0)

#define FAILED (Status) ((HRESULT) (status) <0)

Successful code has multiple, failed code also has more than one function the status code returned in various cases will typically contain multiple success codes and multiple failure codes.

That's why we use succeeded and failed macros. It is generally not possible to compare an HRESULT value directly to a success code (such as S_OK) to determine whether a function succeeds.

Detailed description of HRESULT

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.