BadImageFormatException was unhandled Exception

Recently, when working with Managed DirectX in .NET on a 64 bits system I came across the following error:

BadImageFormatException was unhandled

BadImageFormatException was unhandled

is not a valid Win32 application.  (Exception from HRESULT: 0x800700C1)

  • Make sure the file image is a valid managed assembly.
  • Make sure you have supplied a correct file path for the assembly.
  • Get General help for this exception.

If you ask for more details it gives you the following information:

System.BadImageFormatException was unhandled
  Message=" is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)"
  Source="Something"
  StackTrace:
       at Something.Program.Main()
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Solution - Change the solution's plataform to x86

In my case and from what I could find on the web, this usually happens in 64 bits systems (I'm running Windows Vista Business 64 bits edition). The problem is that the compiler trys to output 64 bits compatible code, but because it doesn't have the necessary assemblies to do that, it fails with BadImageFormatException.

So, the solution is to tell the compiler to ouput code for x86 systems (because most assemblies/librarys usually only support x86 systems). After doing this, the problem should be gone (the x86 produced code should run on 64 bits systems).

Next I will explain the steps for configuring a project's output platform. I will use Visual Studio 2005, but it should be very similar in other versions.

We need to do the following:

  • Go to the solution's properties page
  • Select Configuration Manager
  • Add a new platform to the project (targeted to x86 systems)

First we need to go to the solution's properties page. In the Solution Explorer right-click the solution and select Properties.

Right-Click Solution Properties

The Property Pages dialog appears. Make sure that Configuration Properties is selected on the left side of the window. Click on the Configuration Manager button that appears on the top-right corner of the dialog.

Solution Property Pages

The Configuration Manager pops up. Now you should go to the Platform dropdown list of your project. Click on "<New...>".

Configuration Manager

The New Project Platform window appears. Select "x86" in the New platform dropdown, "<Empty>" in the Copy settings from: field and leave the checkbox off. Click the OK button to close the dialog box.

New Project Platform

The project should now appear with "x86" in the Platform's column in the Configuration Manager. Close the Configuration Manager and click OK to close the Solution Property Pages. The solution should now run without problems.