什么是BadImageFormatException

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

可能的原因

具体而言, 在以下情况下会引发异常:

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

    这可能需要修改Path环境变量或为正确的可执行文件提供完全限定的路径。
  • 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(0, 
                         e.Message.IndexOf(".") + 1));   
    }
    // 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.

    DllImportAttribute或中extern C#包含关键字的属性) 访问在 DLL 中定义的方法。

  • Assembly.ReflectionOnlyLoad方法在只反射上下文中加载引用程序集。
  • 例如, 它依赖于 COM 互操作或在32位动态链接库中调用方法。

  • 下面的示例定义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 = 0; ctr <= words.Length - 1; ctr++)
          {
             isException = false;
    
             foreach (string exception in exceptionList)
             {
                if (words[ctr].Equals(exception) && ctr > 0)
                {
                   isException = true;
                   break;
                }
             }
             
             if (! isException)
                newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
             else
                newWords[ctr] = words[ctr];     
          }    
          return String.Join(" ", newWords);             
       }
    }
    // Attempting to load the StringLib.dll assembly produces the following output:
    //    Unhandled Exception: System.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);
       }
    }
  • 文件列表应在命令行中以空格分隔的列表形式提供。
    using System;
    using System.IO;
    using System.Reflection;
    
    public class Example
    {
       public static void Main()
       {
          String[] args = Environment.GetCommandLineArgs();
          if (args.Length == 1) {
             Console.WriteLine("\nSyntax:   PlatformInfo <filename>\n");
             return;
          }
          Console.WriteLine();
    
          // Loop through files and display information about their platform.
          for (int ctr = 1; 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++可执行文件中保留重定位地址, 请在链接时指定/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。

相关文章:

  • 2022-12-23
  • 2021-05-01
  • 2022-01-24
  • 2021-11-19
  • 2022-01-13
  • 2022-01-31
  • 2021-12-05
猜你喜欢
  • 2022-03-09
  • 2021-07-12
  • 2021-09-18
  • 2022-12-23
  • 2021-04-08
  • 2022-12-23
相关资源
相似解决方案