Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException when using C++/CLI in a C# application

I am currently developing an application in VS 2008, in which I have a unmanaged C++ library (A) that wraps up two existing libraries (B + C) to simplify functionality. (A) has shown to work and I can successfully call and use it in a c++ test application.

The main purpose of (A) is to be used by a C# application, in order to do this I have followed the following link: http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx to create a CLI wrapper (D).

All seems well, everything compiles and I have attempted to test this in a simple C# application. I added a reference to (D) and fired up the program only to receive the BadImageFormatException when trying to use the class. From what I have read people suggest that there is a vesion mismatch in the DLLs somewhere, however I have built them all in Win32 to match each other.

In addition to this I have provided (D) and the C# application a copy of all the DLLs that are required but still seem to get a problem. I have noticed that a dll is not created for (D) when compiled which is my guess at why it is falling apart, however as I am new to this I don't know if it should or not.

Am I likely not including a DLL somewhere that I have overlooked that is causing this?

TLDR: A wraps B + C, A is wrapped by D to be used in a C# application, but it hits and exception. (I have put all DLL where they should go (to my knowledge) and ensured they all match). Are there any gotchas that I may of missed?

like image 630
Rookasaur Avatar asked Apr 25 '14 14:04

Rookasaur


2 Answers

Try to change your compilation mode to x86 or Any CPU Mode.

Right Button click in your solution > Configuration Manager.

Or

Build > Configuration Manager

enter image description here

In the Configuration Manager change all projects to compile in x86 or ANy CPU, try both.

enter image description here

like image 178
Only a Curious Mind Avatar answered Sep 17 '22 02:09

Only a Curious Mind


To the people who are experiencing BadImageFormatException in this same context in 2022:

For me the problem was solved by disabling "Prefer 32 bits" from C# library properties.

like image 39
kaba Avatar answered Sep 20 '22 02:09

kaba