Reputation: 39
HRESULT hr = S_OK; //initialization`
if (int i == 5)
{
hr = 1; //Is it correct usage?
}
if (hr == 1)
cout<<"The value of i is 5";
As I always heard HRESULT
output in strings like E_TIMEOUT
etc. whether it's correct to use a number like I've used.
Upvotes: 2
Views: 8281
Reputation: 114765
The main question here is what are you using the HRESULT
for?
You can obviously assign any value to an HRESULT
but then why not just use an int
?
HRESULT
s are used as a way to communicate success or failure of function calls. Usually an HRESULT
is either S_OK
(0) or an error (negative numbers). S_FALSE
(1, not a failure but not success either) is more rarely used.
When used with COM or .NET the caller usually only checks if the function succeeded or failed. By default when marshaling a function that returns an HRESULT
.NET's marshaller will turn the function into a void
function, throwing an exception if the result is negative and discarding the result if it's non-negative.
If this is the case for your code then there's no point in giving a specific positive value. If you're not using this for interoperability you probably shouldn't be using an HRESULT
at all.
Upvotes: 3
Reputation: 1008
Basically, everything is inside '<winerror.h>'. But, I have this feeling, that might be not a popular answer.
The Context
HRESULT is a handle (value of type long) returned from Win32, COM and other functions in the vast ocean of Windows SDK. What and how is packed in that handle is explained in <winerror.h>
I might dare to think there is one fact about Win32 of great use to C++ developers.
Win32 is written in C
Thus there are no C++ exceptions. Ditto HRESULT is your friend. There are "special exceptions" called SEH (Structured Exceptions Handling). And they are "inbuilt" into the Windows. Even MS STL uses SEH when c++ exceptions are switched off (gasp!). But that was not part of the question.
The usage looks very simple:
HRESULT hr ;
ULONG ansi_size_ ;
hr = DiscpUnicodeToAnsiSize(L"Look Ma, I am doing WIN32!", &ansi_size_ );
if ( FAILED(hr) ) {
// there was some error
// what exactly has happened?
HRESULT facility = HRESULT_FACILITY(hr);
HRESULT severity = HRESULT_SEVERITY(hr);
// we can for example look for a particular facility
if ( facility == FACILITY_MSMQ)
{
// hey, just what is MSMQ ?!
// let see the code part
HRESULT code = HRESULT_CODE(hr);
}
}
<winerror.h>
is (currently) 62238 lines long. Feel free to browse it and you will find a lot of useful nuggets.
Wikipedia article on HRESULT is actually short and to the point.
Win32 is now several decades old. Inevitably there are dusty corners. But HRESULT will stay "forever" and in active development.
As it happens Windows Error Codes Protocol Revision 20 (gasp) has been released just yesterday!
But that is C!?
There have been few official projects (also released) to "encapsulate" HRESULT and COM into C++ types. MFC, ATL ... My favourite is Compiler COM Support, and in particular, the _com_error class.
I hope this has helped ...
Upvotes: 2
Reputation: 104559
An HRESULT of 0
or S_OK
is the standard "success" code. Whereas 1
is S_FALSE, which means, "success, but I didn't exactly do you what you asked because it was probably already done".
Standard pattern is something closer to this:
HRESULT hr = S_OK //initialization
if (int i == 5)
{
hr = S_OK;
}
else
{
hr = E_INVALIDARG; // Or any E_ error code
}
if (SUCCEEDED(hr))
{
cout<<"The value of i is 5";
}
else if (hr == E_INVALIDARG)
{
// handle specific error
}
else
{
// handle all other errors generically
}
Upvotes: 2