简体   繁体   English

C#没有捕获非托管C ++ DLL的未处理异常

[英]C# not catching unhandled exceptions from unmanaged C++ dll

I've got an unmanaged C++ dll which is being called from a C# app, I'm trying to get the C# app to catch all exceptions so that in the event of the dll failing due to an unmanaged exception then the user will get a half-decent error message (the C# app is a web service implementing it's own http handler). 我有一个从C#应用程序调用的非托管C ++ dll,我正在尝试使用C#应用程序来捕获所有异常,以便在由于非托管异常导致dll失败的情况下,用户将得到一个半合适的错误消息(C#app是一个实现它自己的http处理程序的Web服务)。

The problem I have is that not all types are being caught. 我遇到的问题是并非所有类型都被捕获。 So if I create the following and execute the C# app then the dll throws an error and the entire application terminates. 因此,如果我创建以下内容并执行C#应用程序,则dll会抛出错误并终止整个应用程序。 Any ideas? 有任何想法吗?

This is being created in VS2005 and using .Net framework v2 这是在VS2005中使用.Net framework v2创建的

C++ - Test.h C ++ - Test.h

#ifndef INC_TEST_H
#define INC_TEST_H

extern "C" __declspec(dllexport) void ProcessBadCall();

#endif

C++ - Test.cpp C ++ - Test.cpp

#include <iostream>
#include <vector>

using namespace std;

void ProcessBadCall()
{
  vector<int> myValues;
  int a = myValues[1];
  cout << a << endl;
}

C# - Program.cs C# - Program.cs

class Program
{
  [DllImport("Test.dll", EntryPoint="ProcessBadCall")]
  static extern void ProcessBadCall();

  static void Main(string[] args)
  {
    try
    {
      ProcessBadCall();
    }
    catch (SEHException ex)
    {
      Console.WriteLine("SEH Exception: {0}", ex.Message);
    }
    catch (Exception ex)
    {
      Console.WriteLine("Exception: {0}", ex.Message);
    }
  }
}

The dll is being compiled under the release configuration with the following compiler flags. dll在发布配置下使用以下编译器标志进行编译。

/O2 /GL /D "WIN32" /D "NDEBUG" /D "_CRT_SECURE_NO_WARNINGS" /D "_UNICODE" /D "UNICODE" /D "_WINDLL" /FD /EHa /MD /Fo"Release\\" /Fd"Release\\vc80.pdb" /W4 /WX /nologo /c /Wp64 /Zi /TP /errorReport:prompt / O2 / GL / D“WIN32”/ D“NDEBUG”/ D“_CRT_SECURE_NO_WARNINGS”/ D“_UNICODE”/ D“UNICODE”/ D“_WINDLL”/ FD / EHa / MD / Fo“Release \\”/ Fd“发布\\ vc80.pdb“/ W4 / WX / nologo / c / Wp64 / Zi / TP / errorReport:提示

Try catching using the ExternalException class: 尝试使用ExternalException类进行捕获:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.externalexception%28v=VS.100%29.aspx http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.externalexception%28v=VS.100%29.aspx

And, try compiling your unmanaged C++ DLL with asynchronous exception handling (/EHa). 并尝试使用异步异常处理(/ EHa)编译非托管C ++ DLL。 It looks like you're getting a Read Access Violation in your DLL which is a type of async exception. 看起来你的DLL中有一个读取访问冲突,这是一种异步异常。

AFAIK, only .NET v4 and above disables the delivery of async exceptions by default. AFAIK,只有.NET v4及更高版本默认禁用异步异常的传递。 Even then, you could add legacyCorruptedStateExceptionsPolicy=true to the app.config to enable it. 即使这样,您也可以将legacyCorruptedStateExceptionsPolicy = true添加到app.config以启用它。 Prior to that, it's automatically enabled (check that you have got it set to false in app.config). 在此之前,它会自动启用(检查是否已在app.config中将其设置为false)。

Note that it's my personal believe that AVs in your unmanaged DLL is inherently bad (and dangerous) anyway and it's probably the right behavior for .NET to simply terminate the app. 请注意,我个人认为,无论如何,非托管DLL中的AV本质上都是坏的(并且是危险的),并且它可能是.NET直接终止应用程序的正确行为。 Try throwing std::exception instead. 尝试抛出std :: exception。 If you insists on catching async exceptions, the best way would be to have a thunking DLL which wraps try-catch-all around the call to potentially buggy function calls. 如果你坚持捕获异步异常,最好的方法是让一个thunking DLL包装try-catch-all围绕调用潜在的bug函数调用。 Again, highly /not/ recommended (although I can see how it would be useful in debugging the misbehaving DLL). 再次,高度/非/推荐(虽然我可以看到它在调试行为不当的DLL时有用)。

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

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