C# 防止应用程序在dll中出现未处理的异常时崩溃

C# 防止应用程序在dll中出现未处理的异常时崩溃,c#,dll,C#,Dll,我正在开发一个C WPF应用程序。我使用的DLL不是我自己开发的。我是这样使用它的: try { Bitmap bmp = new Bitmap(filePath); // Use the DLL var worker = theDLL.loadImage(bmp); status state = worker.start(); if (state = theDLL.sta.OK) { return "OK" }

我正在开发一个C WPF应用程序。我使用的DLL不是我自己开发的。我是这样使用它的:

try
{
    Bitmap bmp = new Bitmap(filePath);

    // Use the DLL
    var worker = theDLL.loadImage(bmp);

    status state = worker.start();

    if (state = theDLL.sta.OK)
    {
        return "OK"
    }
    else
    {
        state = worker.tryOtherMethod();
    }
}
catch (Exception e)
{
    // Do something with e
}
有时,我并不总是有一个未处理的异常。此异常不是catch和我的应用程序崩溃:

向认为无效的函数传递了无效参数 参数致命

我看到这个异常来自C++代码,但我无法用AppDima.CurrdMulnAn。p> 我怎样才能防止撞车

编辑:

我曾尝试单独使用catch和catch Win32Exception e捕获异常,但两者都没有解决我的问题

我仍然有这个错误:

您可以只编写catch而不编写异常e,这将允许您捕获本机异常

try
{
    Bitmap bmp = new Bitmap(filePath);

    // Use the 
    var worker = theDLL.loadImage(bmp);

    status state = worker.start();

    if (state = theDLL.sta.OK)
    {
        return "OK"
    }
    else
    {
        state = worker.tryOtherMethod();
    }
}
catch (Exception e)
{
    // Exceptions derived from Exception
}
catch
{
    // Native exceptions
}

您有时会遇到同一个文件或不同文件的问题吗?是否有可能将一个损坏的或无效的文件传递给函数?@尼尔,我尝试了很多文件,一次文件可以正常工作,下次在同一次运行或不同的运行中崩溃,是的,C++运行时可以生成这个诊断。它不能被捕获,不会作为异常抛出,并将在整个过程中快速失效。从技术上讲,C++代码可以改变这种行为,但它并不是会产生更好的结果。这始终是一个编程错误,异常无法修复错误。我已经尝试了,但仍然无法捕获异常。您是否在重复问题中尝试了解决方案?是的,但我仍然无法捕获异常