Using Macros for Error Handling

There are several macros for making it easier to work with SCODE values on 16-bit platforms and HRESULT values on both 16- and 32-bit platforms. Some of the macros and functions below provide conversion between the two datatypes and can be used in client and service provider code that:

For 32-bit environments, these datatype conversion macros exist to provide compatibility and to ease the porting effort.

There are two sets of macros that test for failure or success: HR_SUCCEEDED and HR_FAILED and SUCCEEDED and FAILED.

On 32-bit platforms, because SUCCEEDED is the same as HR_SUCCEEDED and FAILED is the same as HR_FAILED, developers that want to write cross-platform clients or service providers are the only ones that need to distinguish between an HRESULT and an SCODE.

In this case, use the ResultFromScode macro to set the HRESULT variable to the corresponding HRESULT value for S_OK.

Warning The macro SCODE_FACILITY retrieves twelve bits from the SCODE in its Win16 implementation when it should only retrieve four. This can cause false negative results to occur when comparing the facility retrieved from a particular SCODE against predefined facility values. Programmers writing cross-platform code need to be aware of this problem.

Commonly used macros are briefly described in the following table.

Macro Description
GetScode Converts an HRESULT into an SCODE.
ResultFromScode Converts an SCODE into an HRESULT.
MAKE_SCODE Constructs an SCODE from its components.
MAKE_HRESULT Constructs an HRESULT from its components.
HR_SUCCEEDED Tests an HRESULT for a success or warning condition.
HR_FAILED Tests an HRESULT for an error condition.
HRESULT_CODE Extracts the error code part of the HRESULT.
HRESULT_FACILITY Extracts the facility from the HRESULT.
HRESULT_SEVERITY Extracts the severity bit from the SEVERITY.
SCODE_CODE Extracts the error code part of the SCODE.
SCODE_FACILITY Extracts the facility from the SCODE.
SCODE_SEVERITY Extracts the severity field from the SCODE.
SUCCEEDED Tests an SCODE for a success or warning condition.
FAILED Tests an SCODE for an error condition.