How to return HRESULT from C# to C++ instead of throwing Exceptions?
-
28-05-2021 - |
Question
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
Solution
In order to return HRESULTS, you need to throw a COMException
, not just any exception.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow