The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.
See Also: BadImageFormatException Members
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 System.Reflection.Assembly.LoadFile(string) method to load Kernel32.dll.
code reference: System.BadImageFormatException.Class#1
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 System.Runtime.InteropServices.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_v10_short or the net_v11_short attempts to load an assembly that was developed using the net_v20SP1_short or later, or when an application that was developed using the net_v20SP1_short or net_v35_short attempts to load an assembly that was developed using the net_v40_short. 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.
code reference: System.BadImageFormatException.Class#2
The following example uses reflection to load an assembly named StringLib.dll. If the source code is compiled with a net_v11_short compiler, a BadImageFormatException is thrown by the System.Reflection.Assembly.LoadFrom(string) method.
code reference: System.BadImageFormatException.Class#3
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.
The following example demonstrates an attempt to load an unmanaged executable, which causes the system to throw a BadImageFormatException exception.
C# Example
using System; using System.Reflection; public class BadImageExample { public static void Main() { try { Assembly a = Assembly.Load("calc"); } catch (BadImageFormatException e) { Console.WriteLine("Caught: {0}", e.Message); } } }
The output is
Caught: The format of the file 'calc' is invalid.