什么是BadImageFormatException

BadImageFormatException是当动态链接库 (DLL) 或可执行程序的文件映像无效时引发的异常。

可能的原因

如果动态链接库 (.dll 文件) 或可执行文件 (.exe 文件) 的文件格式不符合公共语言运行时所需的格式, 则会引发此异常。 具体而言, 在以下情况下会引发异常:

  • 早期版本的 .NET Framework 实用工具 (如 installutil.exe 或) 与使用 .NET Framework 的更高版本开发的程序集一起使用。

    若要解决此异常, 请使用与用于开发程序集的 .NET Framework 版本相对应的工具版本。 这可能需要修改Path环境变量或为正确的可执行文件提供完全限定的路径。

  • 尝试加载非托管动态链接库或可执行文件 (如 Windows 系统 DLL), 就像它是 .NET Framework 程序集一样。
    下面的示例通过使用Assembly.LoadFile方法加载 kernel32.dll 对此进行了说明。
    // Windows DLL (non-.NET assembly)
    string filePath = Environment.ExpandEnvironmentVariables("%windir%");
    if (! filePath.Trim().EndsWith(@"\"))
    filePath += @"\";
    filePath += @"System32\Kernel32.dll"; try {
    Assembly assem = Assembly.LoadFile(filePath);
    }
    catch (BadImageFormatException e) {
    Console.WriteLine("Unable to load {0}.", filePath);
    Console.WriteLine(e.Message.Substring(,
    e.Message.IndexOf(".") + ));
    }
    // The example displays an error message like the following:
    // Unable to load C:\WINDOWS\System32\Kernel32.dll.
    // The module was expected to contain an assembly manifest.

    若要解决此异常, 请使用您的开发语言提供的功能 (如 Visual Basic 中Declare的语句DllImportAttribute或中extern C#包含关键字的属性) 访问在 DLL 中定义的方法。

  • 尝试在非仅反射上下文中加载引用程序集。 可以通过以下两种方式之一解决此问题:
    可以加载实现程序集, 而不是引用程序集;您可以通过调用Assembly.ReflectionOnlyLoad方法在只反射上下文中加载引用程序集。
  • DLL 或可执行文件作为64位程序集加载, 但包含32位功能或资源。 例如, 它依赖于 COM 互操作或在32位动态链接库中调用方法。

    若要解决此异常, 请将项目的 "平台目标" 属性设置为 x86 (而不是 X64 或 AnyCPU) 并重新编译。

  • 应用程序的组件是使用 .NET Framework 的不同版本创建的。 通常, 在使用.NET Framework 1.0 .NET Framework 1.1或更高版本开发的应用程序或组件尝试加载使用.NET Framework 2.0 SP1或更高版本开发的程序集时, 或者当使用或尝试加载使用.NET Framework 4或更高版本开发的程序集。 .NET Framework 3.5 .NET Framework 2.0 SP1 BadImageFormatException可能会被报告为编译时错误, 或者在运行时可能会引发异常。 下面的示例定义StringLib了一个类, 该类包含一个名为 StringLib 的程序集中的成员、 ToProperCase和。
    using System;
    
    public class StringLib
    {
    private string[] exceptionList = { "a", "an", "the", "in", "on", "of" };
    private char[] separators = { ' ' }; public string ToProperCase(string title)
    {
    bool isException = false; string[] words = title.Split( separators, StringSplitOptions.RemoveEmptyEntries);
    string[] newWords = new string[words.Length]; for (int ctr = ; ctr <= words.Length - ; ctr++)
    {
    isException = false; foreach (string exception in exceptionList)
    {
    if (words[ctr].Equals(exception) && ctr > )
    {
    isException = true;
    break;
    }
    } if (! isException)
    newWords[ctr] = words[ctr].Substring(, ).ToUpper() + words[ctr].Substring();
    else
    newWords[ctr] = words[ctr];
    }
    return String.Join(" ", newWords);
    }
    }
    // Attempting to load the StringLib.dll assembly produces the following output:
    // Unhandled Exception: System.BadImageFormatException:
    //

    下面的示例使用反射加载名为 StringLib 的程序集。 如果源代码是用.NET Framework 1.1编译器编译的BadImageFormatException , 则Assembly.LoadFrom方法会引发。

    using System;
    using System.Reflection; public class Example
    {
    public static void Main()
    {
    string title = "a tale of two cities";
    // object[] args = { title}
    // Load assembly containing StateInfo type.
    Assembly assem = Assembly.LoadFrom(@".\StringLib.dll");
    // Get type representing StateInfo class.
    Type stateInfoType = assem.GetType("StringLib");
    // Get Display method.
    MethodInfo mi = stateInfoType.GetMethod("ToProperCase");
    // Call the Display method.
    string properTitle = (string) mi.Invoke(null, new object[] { title } );
    Console.WriteLine(properTitle);
    }
    }
  • 应用程序的组件面向不同的平台。 例如, 你要尝试在 x86 应用程序中加载 ARM 程序集。 您可以使用以下命令行实用程序来确定单个 .NET Framework 程序集的目标平台。 文件列表应在命令行中以空格分隔的列表形式提供。
    using System;
    using System.IO;
    using System.Reflection; public class Example
    {
    public static void Main()
    {
    String[] args = Environment.GetCommandLineArgs();
    if (args.Length == ) {
    Console.WriteLine("\nSyntax: PlatformInfo <filename>\n");
    return;
    }
    Console.WriteLine(); // Loop through files and display information about their platform.
    for (int ctr = ; ctr < args.Length; ctr++) {
    string fn = args[ctr];
    if (! File.Exists(fn)) {
    Console.WriteLine("File: {0}", fn);
    Console.WriteLine("The file does not exist.\n");
    }
    else {
    try {
    AssemblyName an = AssemblyName.GetAssemblyName(fn);
    Console.WriteLine("Assembly: {0}", an.Name);
    if (an.ProcessorArchitecture == ProcessorArchitecture.MSIL)
    Console.WriteLine("Architecture: AnyCPU");
    else
    Console.WriteLine("Architecture: {0}", an.ProcessorArchitecture); Console.WriteLine();
    }
    catch (BadImageFormatException) {
    Console.WriteLine("File: {0}", fn);
    Console.WriteLine("Not a valid assembly.\n");
    }
    }
    }
    }
    }
  • 对 C++ 可执行文件进行反射可能会引发此异常。 这极有可能是因为 C++ 编译器从可执行文件中剥离重定位地址或 .Reloc 节引起的。 若要在C++可执行文件中保留重定位地址, 请在链接时指定/fixed: no。

补充说明

  • 如果您的应用程序使用了 32 位组件,请确保该应用程序始终采用 32 位应用程序的运行方式。如果应用程序项目的**“平台目标”属性设置为 AnyCPU,则编译后的应用程序在 64 位或 32 位模式中均可运行。 如果采用 64 位应用程序运行方式,则实时 (JIT) 编译器便会生成 64 位本机代码。 如果应用程序依赖于某个 32 位托管组件或非托管组件,则在 64 位模式中无法加载该组件。 若要纠正此问题,请将项目的“平台目标”**属性设置为 x86,然后重新编译。
  • 确保未使用利用其他 .NET Framework 版本创建的组件。如果使用 .NET Framework 1.0 或 .NET Framework 1.1 开发的应用程序或组件尝试加载使用 .NET Framework 2.0 SP1 或更高版本开发的程序集,或者使用 .NET Framework 2.0 SP1 或 .NET Framework 3.5 开发的应用程序尝试加载使用 .NET Framework 4 开发的程序集,便会引发此异常。

  • 确保文件映像是有效的托管程序集或模块。当非托管动态链接库或可执行文件传递给 Load 方法进行加载时会引发此异常。
  • BadImageFormatException使用 COR_E_BADIMAGEFORMAT 值为0x8007000B 的 HRESULT。

关于System.BadImageFormatException的更多相关文章

  1. visual studio 调试时遇到 System.BadImageFormatException

    System.BadImageFormatException”类型的未经处理的异常在 未知模块. 中发生 其他信息: 未能加载文件或程序集“SendYourIP.exe”或它的某一个依赖项.生成此程序 ...

  2. System.BadImageFormatException: Could not load file or assembly

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe C:\_PRODUKCIJA\Debug\DynamicHtmlT ...

  3. System.BadImageFormatException : 未能加载文件或程序集“Medici.PaymentRecover, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。

    System.BadImageFormatException : 未能加载文件或程序集“xxxxx.xxxxx, Version=1.0.0.0, Culture=neutral, PublicKey ...

  4. 异常:System.BadImageFormatException,未能加载正确的程序集XXX

    IDE:VS2015 语言:C# 异常:System.BadImageFormatException,未能加载正确的程序集XXX或其某一依赖项... 一般是由于目标程序的目标平台与其某一依赖项的目标编 ...

  5. 問題排查:System.BadImageFormatException: 未能加载文件或程序集“System.ServiceModel

    錯誤訊息如下: System.BadImageFormatException: 未能加载文件或程序集“System.ServiceModel, Version=3.0.0.0, Culture=neu ...

  6. (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

    ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect for ...

  7. system.badimageformatexception 未能加载文件或程序集

    今天在调用dll文件的时候发现这样一个错误.      system.badimageformatexception 未能加载文件或程序集.   发现项目CPU默认Any CPU,我的系统是X64,将 ...

  8. System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

    原文 System.BadImageFormatException: 试图加载格式不正确的程序. (异常来自 HRESULT:0x8007000B) 用C#调用DLL文件,运行后报错如下: Syste ...

  9. “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确

    引用sqlite的程序集时,有时会报如下异常:  "System.BadImageFormatException"类型的未经处理的异常在 PurchaseDevices.Acces ...

  10. jexus System.BadImageFormatException Details: Non-web exception. Exception origin (name of application or object): App_global.asax_ai3fjolq.

    Application ExceptionSystem.BadImageFormatExceptionInvalid method header format 0Description: HTTP 5 ...

随机推荐

  1. Go语言(环境的搭建)

    一步一步,从零搭建Go语言开发环境. 安装Go语言及搭建Go语言开发环境 下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://gol ...

  2. 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76

    package y2019.Algorithm.str.hard; import java.util.Stack; /** * @ProjectName: cutter-point * @Packag ...

  3. The One day 中位数的计算

    """ 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / ...

  4. 【leetcode-11】盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  5. mysql执行出错:Table 'k_user' is read only

    执行sql的时候发现出错 Table 'k_user' is read only 1.给mysql权限 chmod 777 mysql 2.执行一下命令 mysqladmin -uroot -p re ...

  6. 据时间生成唯一序列ID

    据时间生成唯一序列ID /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> ...

  7. MySQL单表数据量过千万,采坑优化记录,完美解决方案

    问题概述 使用阿里云rds for MySQL数据库(就是MySQL5.6版本),有个用户上网记录表6个月的数据量近2000万,保留最近一年的数据量达到4000万,查询速度极慢,日常卡死.严重影响业务 ...

  8. springboot IDEA新建Maven项目的Plugins出现红线的解决方法

    将pom.xml文件copy到桌面,删除项目中的pom.xml.发现项目maven中没有任何东西后,然后将桌面的pom.xml粘贴到项目目录下,刷新maven就ok了

  9. 读取txt文件的数据,并将其转换为矩阵

    import numpy as nppath_txt_data = 'C:/Users/51102/Desktop/my_yolo/data/box/train.txt'def input_data( ...

  10. 【开发工具】- Myeclipse10.7破解方法

    1.下载myeclipse 10,如果没有,可以使用链接:https://pan.baidu.com/s/1l9juqD4ALMuepVL6e5kgjA 密码:kpx6:当然时间久了可能链接失效,如有 ...