不能捕获的异常引起的C DLL通过PInvoke的所谓 [英] Can't catch exception caused by C dll called via PInvoke

查看:289
本文介绍了不能捕获的异常引起的C DLL通过PInvoke的所谓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个C#.NET 3.5程序至极采用了最新的 MediaInfoLib DLL 。照片 看来,导致异常的一些文件。

I'm writing a C# .NET 3.5 program wich uses the latest MediaInfoLib Dll.
It seems that it causes an exception for some files.

我想抓住这些异常,并确保我的程序继续运行,
但由于某些原因,我不能用一个简单的try / catch语句抓住它。

I want to catch those exceptions and ensure my program continues running,
but for some reason I can't catch it with a simple try/catch statement.

PInvoke的方法:

PInvoke Methods:

    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_New();
    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_Open(IntPtr Handle,MarshalAs(UnmanagedType.LPWStr)] string FileName);

用法:

    Handle = MediaInfo_New();
    try{
        MediaInfo_Open(Handle, FileName)
    } catch { } 

调用MediaInfo_Open(手柄,文件名)可能会导致异常。
相反,与try / catch语句捕获错误,我的程序退出,vshost32-clr2.exe崩溃。 (这也崩溃作为一个发布版本,并没有任何附加的调试器)
在网上搜索后,我发现有人谁建议选中启用非托管code调试,这不仅造成我的程序退出,而不vshost32-clr2.exe崩溃。

Calling MediaInfo_Open(Handle, FileName) might cause an exception.
Instead of catching the error with the try/catch statement, my program exits and "vshost32-clr2.exe" crashes. (It also crashes as a release build and with no debugger attached)
After searching the web, I found someone who suggested to check "Enable unmanaged code debugging", which only resulted in my program exiting without vshost32-clr2.exe crashing.

任何想法如何,我可以捕获该异常?

Any idea how I can catch the exception?

推荐答案

如果非托管的DLL是导致飞机失事的原因(而不是只返回某种类型的错误code),那么有没有办法抓住它。一旦你已经走了.NET运行时的控制范围之外,这是完全由非托管code;没有什么.NET运行时能做到。

If the unmanaged DLL is causing the crash (rather than just returning an error code of some kind), then there's no way to catch it. Once you've gone outside of the .NET runtime's control, it's entirely up to the unmanaged code; there's nothing the .NET runtime can do.

这篇关于不能捕获的异常引起的C DLL通过PInvoke的所谓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆