Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the windows return code called HRESULT?

Tags:

c

windows

hresult

The standard return type for functions in Windows C/C++ APIs is called HRESULT.

What does the H mean?

like image 995
numerodix Avatar asked Nov 18 '14 18:11

numerodix


People also ask

What does HRESULT stand for?

All COM functions and interface methods return a value of the type HRESULT, which stands for 'result handle'. HRESULT returns success, warning, and error values. HRESULTs are 32-bit values with several fields encoded in the value.

What is HRESULT in Windows?

HRESULT is a computer programming data type that represents the completion status of a function. It is used in the source code of applications targeting Microsoft Windows and earlier IBM/Microsoft OS/2 operating systems, but its design does not limit its use to these environments.

What type is HRESULT?

The HRESULT data type is the same as the SCODE data type. An HRESULT value consists of the following fields: A 1-bit code indicating severity, where zero represents success and 1 represents failure. A 4-bit reserved value.

What is HRESULT error?

The 32bits in an HRESULT error code have meanings, allowing the reader to gain additional insights into the error. Of note: The 32nd bit (the top bit) indicates if an error occurred or not. This is why errors are 0x8xxxxxxx.


1 Answers

The H-prefix in Windows data types generally designates handle types1 (such as HBRUSH or HWND). The documentation seems to be in agreement, sort of:

The HRESULT (for result handle) is a way of returning success, warning, and error values. HRESULTs are really not handles to anything; they are only values with several fields encoded in the value.

In other words: Result handles are really not handles to anything. Clearly, things cannot possibly have been designed to be this confusing. There must be something else going on here.

Luckily, historian Raymond Chen is incessantly conserving this kind of knowledge. In the entry aptly titled Why does HRESULT begin with H when it’s not a handle to anything? he writes:

As I understand it, in the old days it really was a handle to an object that contained rich error information. For example, if the error was a cascade error, it had a link to the previous error. From the result handle, you could extract the full history of the error, from its origination, through all the functions that propagated or transformed it, until it finally reached you.

The document concludes with the following:

The COM team decided that the cost/benefit simply wasn’t worth it, so the HRESULT turned into a simple number. But the name stuck.

In summary: HRESULT values used to be handle types, but aren't handle types any more. The entire information is now encoded in the value itself.


Bonus reading:

Handle types losing their reference semantics over time is not without precedent. What is the difference between HINSTANCE and HMODULE? covers another prominent example.


1Handle types store values where the actual value isn't meaningful by itself; it serves as a reference to other data that's private to the implementation.

like image 59
IInspectable Avatar answered Sep 19 '22 20:09

IInspectable