我有一个C#应用程序,它调用C++ DLL中的函数.此函数可以抛出继承的各种异常std::exception.我目前抓住这样的例外:
try
{
//Call to C++ dll
}
catch (System.Exception exception)
{
//Some error handling code
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是这个代码会抓住所有std::exception吗?我的第二个问题是我如何检索std::exception::what字符串,如果我检查exception.Message我只得到"外部组件已抛出异常".
编辑:有问题的函数是在非托管C++ DLL中,并在C#类中导入如下:
[DllImport("SomeDLL.dll")]
public extern static void SomeFunction();
Run Code Online (Sandbox Code Playgroud)
小智 0
怎么打电话?CLR 并没有真正“获取”C++ 异常处理。如果通过 COM 调用 C++ 代码,请添加一个捕获 std::Exception 的层并用 HRESULT/IErrorInfo 包装它。如果通过托管 C++ 调用它,请添加一个将其包装在托管 System.Exception 等中的层。