BadImageFormatException Class
Updated: April 2009
The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.
Assembly: mscorlib (in mscorlib.dll)
This exception is thrown when the file format of a dynamic link library (.dll file) or an executable (.exe file) does not conform to the format that is expected by the common language runtime. In particular, the exception is thrown under the following conditions:
An earlier version of a .NET Framework utility, such as ILDasm.exe or installutil.exe, is used with an assembly that was developed with a later version of the .NET Framework.
To address this exception, use the version of the tool that corresponds to the version of the .NET Framework that was used to develop the assembly. This may require modifying the Path environment variable or providing a fully qualified path to the correct executable.
An attempt is made to load an unmanaged dynamic link library or executable (such as a Windows system DLL) as if it were a .NET Framework assembly. The following example illustrates this by using the Assembly.LoadFile method to load Kernel32.dll.
' Windows DLL (non-.NET assembly) Dim filePath As String = Environment.ExpandEnvironmentVariables("%windir%") If Not filePath.Trim().EndsWith("\") Then filepath += "\" filePath += "System32\Kernel32.dll" Try Dim assem As Assembly = Assembly.LoadFile(filePath) Catch e As BadImageFormatException Console.WriteLine("Unable to load {0}.", filePath) Console.WriteLine(e.Message.Substring(0, _ e.Message.IndexOf(".") + 1)) End Try ' 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.
To address this exception, access the methods defined in the DLL by using the features provided by your development language, such as the Declare statement in Visual Basic or the DllImportAttribute attribute with the extern keyword in C#.
A DLL or executable is loaded as a 64-bit assembly, but it contains 32-bit features or resources. For example, it relies on COM interop or calls methods in a 32-bit dynamic link library.
To address this exception, set the project's Platform target property to x86 (instead of x64 or AnyCPU) and recompile.
Components have been created using different versions of the .NET Framework. Typically, this exception occurs when an application or component that was developed using the .NET Framework 1.0 or the .NET Framework 1.1 attempts to load an assembly that was developed using the .NET Framework 2.0 SP1 or later. The BadImageFormatException may be reported as a compile-time error, or the exception may be thrown at run time. The following example illustrates this scenario. It defines a StringLib class that has a single member, ToProperCase, and that resides in an assembly named StringLib.dll.
Public Module StringLib Private exceptionList() As String = { "a", "an", "the", "in", "on", "of" } Private separators() As Char = { " "c } Public Function ToProperCase(title As String) As String Dim isException As Boolean = False Dim words() As String = title.Split( separators, StringSplitOptions.RemoveEmptyEntries) Dim newWords(words.Length) As String For ctr As Integer = 0 To words.Length - 1 isException = False For Each exception As String In exceptionList If words(ctr).Equals(exception) And ctr > 0 Then isException = True Exit For End If Next If Not isException Then newWords(ctr) = words(ctr).Substring(0, 1).ToUpper() + words(ctr).Substring(1) Else newWords(ctr) = words(ctr) End If Next Return String.Join(" ", newWords) End Function End Module
The following example uses reflection to load StringLib.dll. If the source code is compiled with a .NET Framework 1.1 compiler, a BadImageFormatException is thrown by the Assembly.LoadFrom method.
Imports System.Reflection Module Example Public Sub Main() Dim title As String = "a tale of two cities" ' Load assembly containing StateInfo type. Dim assem As Assembly = Assembly.LoadFrom(".\StringLib.dll") ' Get type representing StateInfo class. Dim stateInfoType As Type = assem.GetType("StringLib") ' Get Display method. Dim mi As MethodInfo = stateInfoType.GetMethod("ToProperCase") ' Call the Display method. Dim properTitle As String = CStr(mi.Invoke(Nothing, New Object() { title } )) Console.WriteLine(properTitle) End Sub End Module ' Attempting to load the StringLib.dll assembly produces the following output: ' Unhandled Exception: System.BadImageFormatException: ' The format of the file 'StringLib.dll' is invalid.
To address this exception, ensure that the assembly from which the exception is thrown attempts to load an assembly that was developed by using a compatible version of the .NET Framework.
BadImageFormatException uses the HRESULT COR_E_BADIMAGEFORMAT, which has the value 0x8007000B.
For a list of initial property values for an instance of BadImageFormatException, see the BadImageFormatException constructors.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.