Is the exception message raised by a C# COMponent

2019-06-24 04:18发布

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?

2条回答
聊天终结者
2楼-- · 2019-06-24 04:53

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
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-24 04:56

This is generally possible when implementing the interface ISupportErrorInfo

查看更多
登录 后发表回答