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
?
It's little known that the
IErrorInfo
object (returned byGetErrorInfo
) also implements the_Exception
COM interface, which exposes the public members of theSystem.Exception
class to unmanaged code, includingStackTrace
, etc.To access
_Exception
from C++, you'd need to import a correct version ofmscorlib.tlb
, e.g.:This is generally possible when implementing the interface ISupportErrorInfo