BadImageFormatException was unhandled
This tutorial will describe the BadImageFormatException that arises in some .NET applications and show you a solution to this problem.
Problem - BadImageFormatException was unhandled
Recently, when working with Managed DirectX in .NET on a 64 bits system I came across the following error:

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.

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.
![]()
The Configuration Manager pops up. Now you should go to the Platform dropdown list of your project. Click on "<New...>".
![]()
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.

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.
Comments
09:43 27-03-2009RobertGraauw
Thnx. It worked for me.22:11 17-04-2009ThankfulCoder
Thanks a bunch -- I had no idea it was looking for x86 specifically!20:45 10-02-2010Anil
Thanks for the article. it solved my problem.13:03 01-03-2010MREALE
Man, it worked like a charm. Well done and thank you !14:24 07-04-2010gadi
it made a big mess00:05 05-05-2010OKTAY
Thanks a lot ,it s very useful informationn..23:19 24-06-2010sleepzero
Thank u. It worked for me. 5 stars for you senior!!!!!09:39 14-07-2010Cronky
Thanks mate, i've wasted about 3 hours on this today. You saved me from going crazy.17:13 28-07-2010Phil
A M A Z I N G. Searched for hours, saved my day. It means none of the search engines were accurate enough.Your entry should have come 1st in all SE for this error!
Great! Thanks!
11:05 30-07-2010Mo1963
Hi Friends,I'm an VB6 developper and I want to learn VB.NET with VSS 2010. Can someone tell where I can find an ebook for VB6 developpers?
Thanks.


18:11 12-02-2009chungman5
Thank You!!