Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What values to return for S_OK or E_FAIL from c# .net code?

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;
like image 685
Rory Avatar asked Dec 12 '08 14:12

Rory


3 Answers

VSConstants might do you want you want:

VSConstants.S_OK Field

Generic HRESULT for success.

Namespace: Microsoft.VisualStudio

Assembly: Microsoft.VisualStudio.Shell.10.0 (in Microsoft.VisualStudio.Shell.10.0.dll)

like image 184
basiphobe Avatar answered Nov 15 '22 23:11

basiphobe


E_FAIL is Hex 80004005 in WinError.h

You can see the full Common HRESULT Values. You don't have to install C++ just to see the values.

UPDATE:

The signed and unsigned versions of 0x80004005 are just two representations of the same bit mask. If you're getting a casting error then use the negative signed value. When casted to an UN signed long it will be the "correct" value. Test this yourself in C#, it'll work e.g.

This code

    static void Main(string[] args)
    {
        UInt32 us = 0x80004005;
        Int32 s = (Int32)us;

        Console.WriteLine("Unsigned {0}", us);
        Console.WriteLine("Signed {0}", s);
        Console.WriteLine("Signed as unsigned {0}", (UInt32)s);

        Console.ReadKey();
    }

will produce this output

  • Unsigned 2147500037
  • Signed -2147467259
  • Signed as unsigned 2147500037

So it's safe to use -2147467259 for the value of E_FAIL

like image 43
Binary Worrier Avatar answered Nov 16 '22 00:11

Binary Worrier


From WinError.h for Win32

#define E_FAIL _HRESULT_TYPEDEF_(0x80004005L)

To find answers like this, use visual studio's file search to search the header files in the VC Include directory of your visual studio install directory.

C:\Program Files\Microsoft Visual Studio 9.0\VC\include
like image 24
Rob Prouse Avatar answered Nov 15 '22 22:11

Rob Prouse