tags:

views:

133

answers:

4

Definition of SUCCEEDED(): #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)

Background: When an Ok button is clicked on a dialog, I need to return an HRESULT value hr such that SUCCEEDED(hr) is true. If Cancel button is clicked, I need to return a negative value. I could have used bools, but that would break the existing pattern (usually the hr values come from depths of system dlls). So, I know I can return S_OK on Ok, but what do I return on Cancel? I could just return (HRESULT)-1;, but there must be a better way - some HRESULT literal constant which has negative value and represents a generic failure. S_FALSE is not it, for it's value is defined as 1L.

Please help me find the right constant.

+5  A: 

Perhaps E_ABORT

Kyle Alons
Thanks ... so all E_* values would fail the `SUCCEEDED(hr)` ?
Hamish Grubijan
Yes, they would.
Kyle Alons
+1  A: 

Typical values are shown here: http://msdn.microsoft.com/en-us/library/aa378137(VS.85).aspx

E_FAIL or E_ABORT seem the most obvious.

morechilli
+4  A: 

E_FAIL or E_ABORT. However, this just brings up a larger issue which is you should never use SUCCEEDED(hr) if you just want to check against S_OK.

MSN
+1. Agreed, using SUCCEEDED is wrong here. S_FALSE is the logical return value for a canceled dialog, so compare to S_OK.
Hans Passant
+1  A: 

As Kyle Alons said, E_ABORT (or E_FAIL) might work well for your purpose or you can devise your own using the MAKE_HRESULT() macro or HRESULT_FROM_WIN32() if there's a Win32 error code that matches what you want to indicate.

Maybe HRESULT_FROM_WIN32( ERROR_CANCELLED)?

Michael Burr