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

谁说胖子不能爱 提交于 2019-12-10 15:57:17

问题


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?


回答1:


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



回答2:


This is generally possible when implementing the interface ISupportErrorInfo



来源:https://stackoverflow.com/questions/33612766/is-the-exception-message-raised-by-a-c-sharp-component-passed-through-interop-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!