zoukankan      html  css  js  c++  java
  • 关于C# 调用 C dll时,抓获C的异常

      最近一直被C# 调用native code时的内存错误,各种错误所困扰。而且在.net 4.0中非托管代码的异常不能被托管代码抓获,导致程序直接crash。

      最终找到了.net 的方法。MSDN有关于expection 的详细解释

      1. 在.net 3.5中是可以抓取AccessViolationException这个C崩溃的异常的,因此可以尝试使用这个.net 3.5重新编译

      2.在.net 4,4.5中可以尝试在app.config 文件中加上如下的配置,

          

          <configuration>
            <runtime>
               <legacyCorruptedStateExceptionsPolicy enabled="true"/>
            </runtime>

         <configuration>

      3.为函数标记[HandleProcessCorruptedStateExceptions]

        

    // This program runs as part of an automated test system so you need 
    // to prevent the normal Unhandled Exception behavior (Watson dialog). 
    // Instead, print out any exceptions and exit with an error code. 
    [HandleProcessCorruptedStateExceptions] 
    [SecurityCritical]
    public static int Main() 
    { 
       try
         {
           // Catch any exceptions leaking out of the program CallMainProgramLoop(); 
         }
       catch (Exception e) 
           // We could be catching anything here 
         {
             // The exception we caught could have been a program error
            // or something much more serious. Regardless, we know that
            // something is not right. We'll just output the exception 
           // and exit with an error. We won't try to do any work when
           // the program or process is in an unknown state!
    
            System.Console.WriteLine(e.Message); 
            return 1; 
         } 
    
      return 0; 
    
    }

    但是我使用第二种方法,仍然未解决问题,留待解决。


  • 相关阅读:
    linux下错误的捕获:errno和strerror的使用
    三角识别注意事项
    关于udo3d双目相机的嵌入式板子系统重装
    为网页背景添加一个跟随鼠标变幻的动态线条
    API工具下载地址记录一下
    Eclipse 安装 SVN 插件的两种方法
    java技术面试之面试题大全
    烧绳子问题
    Java web 项目 web.xml 配置文件加载过程
    mysql绿色版安装配置
  • 原文地址:https://www.cnblogs.com/stupidhod/p/3791022.html
Copyright © 2011-2022 走看看