简体   繁体   English

如何处理C ++加载的C#DLL中的异常

[英]how to handle exceptions in C# DLL loaded by C++

I have a DLL that is created in C# for the purpose of providing a COM interface to a third-party C# library. 我有一个在C#中创建的DLL,目的是为第三方C#库提供COM接口。 I have a C++ program that uses that COM interface so that it can communicate with the C# library. 我有一个使用该COM接口的C ++程序,以便它可以与C#库进行通信。 Sometimes, exceptions get thrown on the C# side and all I get back on the C++ side is an HRESULT from the COM invocation that says means "exception occurred" (or something to that effect). 有时,异常会在C#端被抛出,而我在C ++端返回的所有内容都是来自COM调用的HRESULT,表示“异常发生”(或类似的东西)。 Is there any way for me to get the entire exception information so that I can print an informative message to my C++ app's log? 有没有办法让我获取整个异常信息,以便我可以在我的C ++应用程序日志中打印信息性消息? If not, is there anything I can do on the C# side to intercept all exceptions before they trigger whatever mechanism returns the HRESULT to C++ so that I can log them on the C# side? 如果没有,是否有什么我可以在C#端拦截所有异常,然后它们触发任何机制将HRESULT返回到C ++,以便我可以在C#端登录它们?

There is a mapping of a managed exception to an HRESULT. 有一个托管异常到HRESULT的映射。 Consult the table in this MSDN article . 请参阅此MSDN文章中的表。 .NET retrieves error info from a COM object with IErrorInfo. .NET使用IErrorInfo从COM对象检索错误信息。 That might well work the other way around too. 反过来也可能相反。 Worth a shot. 值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM