Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the exception message raised by a C# COMponent passed through interop to C++?

If I implement a COM interface in a C# class, which will be called from a C++ module, then in case of a failure the C# code should throw an exception; this will be marshalled to a HRESULT failure code by the interop layer.

But will any message text (or other information) passed to the Exception constructor also be available to the calling C++ code? If so, how can it be obtained i.e. which API methods?

e.g. if in my C# code I do throw new ArgumentException("The email address is invalid") I would except my COM HRESULT to be E_INVALIDARG but will my custom text be marshalled or can I only look up a text description of E_INVALIDARG?

like image 554
Mr. Boy Avatar asked Nov 10 '22 02:11

Mr. Boy


1 Answers

It's little known that the IErrorInfo object (returned by GetErrorInfo) also implements the _Exception COM interface, which exposes the public members of the System.Exception class to unmanaged code, including StackTrace, etc.

To access _Exception from C++, you'd need to import a correct version of mscorlib.tlb, e.g.:

#import "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb" raw_interfaces_only
like image 194
noseratio Avatar answered Nov 14 '22 22:11

noseratio