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; 
    
    }

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


  • 相关阅读:
    设计模式之抽象工厂模式
    MQ任意延时消息(三)基于服务端实现
    MQ任意延时消息(二)基于客户端实现
    MQ任意延时消息(一)实现原理概述
    sqlyog报错2058
    base标签的作用
    相对路径和绝对路径的解释
    自定义Tomcat部署目录
    常用正则表达式
    接口的结构定义
  • 原文地址:https://www.cnblogs.com/stupidhod/p/3791022.html
Copyright © 2011-2022 走看看