Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 602
Eric Anastas Avatar asked Oct 02 '22 21:10

Eric Anastas


People also ask

How do you fix an attempt was made to load a program with an incorrect format exception from Hresult 0x8007000B?

(Exception from HRESULT: 0x8007000B) Print. Solution: This issue arises when there is a 32 bit/64 bit mismatch between the Argon2 DLL and the application that you are using. Basically you will need to ensure that if you are targeting 64 bit you're using the 64 bit binary and vice versa.

How do you fix an attempt was made to load a program with an incorrect format?

An attempt was made to load a program with an incorrect format. In other words, your libraries are the wrong 'bitness'. Recompile your app for x86 would be the quickest solution here. That will force it to run as a 32-bit process on your 64-bit box.

What is System Badimageformatexception?

This exception is thrown when the file format of a dynamic link library (. dll file) or an executable (.exe file) doesn't conform to the format that the common language runtime expects.

Could not be opened -- An attempt was made to load a program with an incorrect format?

“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.


1 Answers

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.

like image 68
Simon Mourier Avatar answered Oct 11 '22 16:10

Simon Mourier