The hresult returned from the com method is a 32-bit signed integer. Its non-negative value indicates success, and the negative value indicates failure. Hresult has three fields: Level (indicating success or failure), function code, and status code. The function code indicates the components or Program . Microsoft assigns function codes to different components, such as COM and task scheduling programs. The function code is a 16-bit value. It only has no other internal meaning. It is randomly associated between numbers and meanings. It is similar to the value returned by getlasterror.
If you find an error in the winerror. h header file Code , You will see many hresult values listed according to the [function] _ [degree] _ [description] naming convention, and the general hresult (like e_outofmemory) returned by the component does not have a function code in the name. For example:
Regdb_e_readregdb:
Function Code = regdb, which refers to "registry database )";
Degree = e indicates an error );
Description = readregdb is a description of the error (meaning the registry database cannot be read ). S_ OK: no function code -- General (generic)
Hresult;
Degree = s; success );
"OK" indicates that everything is fine (everything's OK ).
1. Error lookup tool
You can use the error lookup tool provided by VC to easily find the built-in function code for hresult. For example, assume that you forgot to call coinitialize () before cocreateinstance (). The value returned by cocreateinstance () is 0x800401f0. You only need to enter this value to the error search tool and press the "look up" button to see the error message description "coinitialize not called", as shown in:
2. Watch window query:
Another method to find the description of hresult is in the debugger. Assume that the hresult variable is hres. Enter "hres, HR" in the Left box of the Watch window to indicate the value to be viewed. "HR" will notify VC to display the value described by hresult. As shown in: