System.BadImageFormatException An attempt was made to load a program with an incorrect format

Eric Anastas picture Eric Anastas · Dec 2, 2010 · Viewed 109.2k times · Source

I'm writing a plug-in for another program that is based on a public .NET API. Typically these plugins are made by creating a class library DLL that references the API assembly. Then a command class is created by inheriting from a base command class in the API assembly. The application is then set to reference the plug-in DLL file, and is then also responsible for actually firing up the custom command class when the user requests it.

However, now I'm trying to automate some code generation through System.CodeDOM, and want to create a simple console application that automatically generates new Class Types based off of types with in the API assembly.

Yet, when I try to run my application I get the following exception.

System.BadImageFormatException was unhandled Message: Could not load file or assembly 'RevitAPI, Version=2011.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Usually I need to set the target framework of a plug-in assembly to 3.5. Yet now I've found that the error above goes away if I set the target framework of my console application to 2.0. However, my console application already references other class libraries of mine that have their target framework set to 3.5. And I'd really rather not rewrite them around the 2.0 framework.

Answer

Simon Mourier picture Simon Mourier · Dec 2, 2010

It's possibly a 32 - 64 bits mismatch.

If you're running on a 64-bit OS, the Assembly RevitAPI may be compiled as 32-bit and your process as 64-bit or "Any CPU".

Or, the RevitAPI is compiled as 64-bit and your process is compiled as 32-bit or "Any CPU" and running on a 32-bit OS.