4

Currently i am writing a C# COM Server which will be used by a C++ client. I want to return a certain HRESULT to the C++ code when an specific exception is raised in the C# code like the following

// C# COM Server Code
public void MyMethod()
{
    try
    {
        //Some Processing 
    }
    catch(CertainException e)
    {
        //I pass in my Own HResult int to the my Custom Exception 
        MyCustomException ex = new MyCustomException(unchecked((int)0xc00491fe)); 
        throw ex;
    }
}

//C++ Code
HRESULT hr = pMyComInstance->MyMethod();

I expect that the "hr" i got from my C++ code is the int i assigned in my C# code, however instead of getting the HRRESULT, i still got the exception in C++ code.

Have i done anything wrong? please help.. Thanks

Chris Benard
  • 3,167
  • 2
  • 29
  • 35
wsw
  • 831
  • 7
  • 9

1 Answers1

7

In order to return HRESULTS, you need to throw a COMException, not just any exception.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964