尽量不赶例外的DllImport功能

尽量不赶例外的DllImport功能

问题描述:

我调用C从C#项目++函数:

I call C++ function from C# project:

[System.Runtime.InteropServices.DllImport("C.dll")]
public static extern int FillSlist(out string slist);



然后

and then

try
{
  FillSlist(out slist);
}
catch
{
}



C ++ dll是由第三方工具保护,所以是真正执行之前FillSlist正在执行一些代码。而执行此第三方代码的东西真的不好的事情发生,程序停止工作的。无论是试一试分离执行的问题,也不是AppDomain.CurrentDomain.UnhandledException。

C++ dll is protected by third-party tool, so some code is being performed before FillSlist is really executed. Something really bad happens while this third-party code is executed and the program stops working at all. Neither "try" isolates the problem nor "AppDomain.CurrentDomain.UnhandledException" is executed.

有什么可以帮助从C#调用代码隔离C ++函数的碰撞?

Is there anything that can help to isolate crash of C++ function from C# calling code?

这是上运行CLR 4.0吗?如果是这样的...

Is this running on CLR 4.0? If so ...

如果一个异常没有被夹在一个开放的catch块在代码中证明这是因为CLR认为它是一个损坏状态异常,是默认不被用户代码处理。相反,它传播并导致进程终止。

If an exception does not get caught in an open catch block as demonstrated in your code it's because the CLR considers it a corrupted state exception and is by default not handled by user code. Instead it propagates up and causes the process to terminate.

它这样做是有原因的,这些类型的异常没有动作托管代码可以采取的纠正问题。唯一可能的解决方案是终止进程。

It does this for a reason for these types of exceptions there is no action managed code can take to correct the problem. The only possible solution is to terminate the process.

您可以通过添加 HandledCorruptedStateException 属性的方法来覆盖此行为。但总的来讲,这是一个坏主意。

You can override this behavior by adding an HandledCorruptedStateException attribute to the method. But generally speaking this is a bad idea.

详细信息

  • http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

如果没有,那么有可能该方案只需在本地代码和执行撞车出局始终无法正常返回到托管代码。

If not then it's possible the program is simply crashing out in native code and execution never properly returns to managed code.