C#不要再追从非托管C未处理的异常++ DLL [英] C# not catching unhandled exceptions from unmanaged C++ dll

查看:306
本文介绍了C#不要再追从非托管C未处理的异常++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非托管C ++ DLL正在被从C#应用程序调用时,我试图让C#应用程序来捕获所有异常,以便在DLL中失败的情况下,由于非托管异常,那么用户将获得一个半体面的错误信息(在C#应用程序是实现它自己的HTTP处理程序的Web服务)。



我的问题是,不是所有的类型都被抓。所以,如果我创建以下和执行C#应用程序,然后该DLL将引发错误和整个应用程序终止。 ?任何想法



这是在VS2005中创建和使用.NET Framework V2



C ++ - Test.h

 的#ifndef INC_TEST_H 
的#define INC_TEST_H

的externC__declspec(dllexport)的无效ProcessBadCall();

#ENDIF



C ++ - Test.cpp的



 的#include<&iostream的GT; 
&#包括LT;矢量>

使用命名空间std;

无效ProcessBadCall()
{
矢量<&INT GT; myValues;
INT一个= myValues [1];
COUT<< A<< ENDL;
}



C# - Program.cs的

 类节目
{
函数[DllImport(Test.dll的入口点=ProcessBadCall)]
静态外部无效ProcessBadCall() ;

静态无效的主要(字串[] args)
{

{
ProcessBadCall();
}
赶上(SEHException前)
{
Console.WriteLine(SEH异常:{0},ex.Message);
}
赶上(异常前)
{
Console.WriteLine(异常:{0},ex.Message);
}
}
}



该DLL被下编译发布配置与以下编译器标志。




/ O2 / GL / DWIN32/ DNDEBUG/ D
_CRT_SECURE_NO_WARNINGS/ D
_UNICODE/ DUNICODE/ D_WINDLL
/ FD / EHA / MD / FORelease\
/ FDRelease\ vc80.pdb/ W4 / WX / NOLOGO
/ C / Wp64 /紫/ TP / errorReport:提示



解决方案

尝试使用 ExternalException 类捕:



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



和,尝试编译非托管C ++ DLL用异步异常处理(/ EHA)。
看起来你要在你的DLL中的读访问冲突这是一种异步异常。



据我所知,只有.NET V4及以上禁用交货默认异步异常。即使是这样,你可以添加legacyCorruptedStateExceptionsPolicy = true添加到的app.config来启用它。在此之前,它会自动启用(请确认你已完成了设置在app.config中假)。



请注意,这是我个人相信,在您的非托管的DLL的AV本质上是坏的(和危险),反正它可能是正确的行为.NET简单地终止应用程序。试着抛出的std ::例外吧。如果你坚持抓异步异常,最好的办法是将有它包装的try-catch-各地调用潜在的马车函数调用的thunk DLL。再次,高度/不/推荐(虽然我可以看到它是如何在调试中行为不端的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).

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. Any ideas?

This is being created in VS2005 and using .Net framework v2

C++ - Test.h

#ifndef INC_TEST_H
#define INC_TEST_H

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

#endif

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

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.

/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

解决方案

Try catching using the ExternalException class:

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). It looks like you're getting a Read Access Violation in your DLL which is a type of async exception.

AFAIK, only .NET v4 and above disables the delivery of async exceptions by default. Even then, you could add legacyCorruptedState­­ExceptionsPolicy=true to the app.config to enable it. Prior to that, it's automatically enabled (check that you have got it set to false in app.config).

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. Try throwing std::exception instead. 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. Again, highly /not/ recommended (although I can see how it would be useful in debugging the misbehaving DLL).

这篇关于C#不要再追从非托管C未处理的异常++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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