Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is HRESULT 0 for success?

Tags:

c++

windows

I just ran into something very awkward while working with HRESULT return values, it seems that success is 0 and failure is 1. What is the logic behind this?

I actually tried if(!hr) and failed miserably, wasting an hour of my life until I figured out the actual success retval is 0. I would like to call the person who thought of this an idiot, but I'll try to cool down - hoping someone will shed some light on this convention.

like image 577
Fractal Resurgence Avatar asked Oct 12 '12 05:10

Fractal Resurgence


2 Answers

The original purpose of HRESULTs was to formally lay out ranges of error codes for both public and Microsoft internal use in order to prevent collisions between error codes in different subsystems of the OS/2 Operating System.

That's why, value of 0 (of highest bit of HRESULT) means 'no error', that is, success.

But one should be careful, since it is possible for HRESULT to have highest bit set to 0 and other bits set different from 0. That means, the check if(0 == hr) { ... } may not work for some successful cases. The correct way to check for success is if(0 <= hr) { ... }.

Wikipedia has more detailed information.

SUCCEEDED and FAILED macros can be used to avoid mistakes when dealing with HRESULT values. if(0 <= hr) { ... } and if(SUCCEEDED(hr)) { ... } are basically the same things.

like image 66
bhovhannes Avatar answered Oct 23 '22 08:10

bhovhannes


You are mistaking again, for all system errors in almost all systems return value of 0 is success and non-zero return values indicate an error code, using this technique we can report different kind of error with just one return value. But for HRESULT we have >= 0 for success and <0 for error, so value of 1 from the view of HRESULT is success not an error. in HRESULT >0 indicate a warning or information about function, 0 means absolute success and <0 indicate an error. Layout of HRESULT is:

 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+-+-+-+-+-+---------------------+-------------------------------+
|S|R|C|N|r|    Facility         |               Code            |
+-+-+-+-+-+---------------------+-------------------------------+
where
  S - Severity - indicates success/fail
      0 - Success
      1 - Fail (COERROR)
  R - reserved portion of the facility code, corresponds to NT's second severity bit.
  C - reserved portion of the facility code, corresponds to NT's C field.
  N - reserved portion of the facility code. Used to indicate a mapped NT status value.
  r - reserved portion of the facility code. Reserved for internal use. Used to 
        indicate HRESULT values that are not status values, but are instead message
        ids for display strings.
  Facility - is the facility code
  Code - is the facility's status code

As you can see this is one of best design with very reasonable support for custom error codes, warning and information

like image 4
BigBoss Avatar answered Oct 23 '22 08:10

BigBoss