I'm implementing a COM interface that should return int values either S_OK
or E_FAIL
. I'm ok returning S_OK
as I get that back from another call (Marshal.QueryInterface), but if I want to return a failure value what actual value do I use for E_FAIL
?
(It's such a basic fundamental question that it's hard to find an answer to)
Assuming it's a specific number defined in the Win32 API, is there way to use it within .net code without declaring my own constant?
thanks!
Update (answered below):
Maybe I'm being a complete plonker, but I'm having problems with this. According to my Platform SDK, HRESULT is a LONG, which is a 32-bit signed integer, right? So possible values –2,147,483,648 to 2,147,483,647. But 0x80004005 = 2,147,500,037 which is > 2,147,483,647. What gives!?
This means when I try to put this in my code:
const int E_FAIL = 0x80004005;
I get a compiler error Cannot implicitly convert type 'uint' to 'int'.
Update 2:
I'm going to declare it like this:
const int E_FAIL = -2147467259;
because if I try to do something like this:
const UInt32 E_FAIL = 0x80004005;
return (Int32)E_FAIL;
I get a compiler error Constant value '2147500037' cannot be converted to a 'int' (use 'unchecked' syntax to override)
Phew! Who knew how tricky it would be to declare a standard return value.... Somewhere there must be a class lurking that I should have used like return Win32ReturnCodes.E_
FAIL; ... sigh
ULTIMATE SOLUTION:
I now do this by getting the (massive but very useful) HRESULT enum from pinvoke.net and adding it to my solution. Then use it something like this:
return HRESULT.S_OK;