I've got a C# 2.0 project which is set to target 'Any Cpu', however it is referencing a C++ project that's building a 32 bit dll.
When I try to run my program on a 64bit machine I get the following error:
System.BadImageFormatException
was unhandled Message: Could not load file or assemblyTreeTMHook, Version=1.0.2889.19619, Culture=neutral, PublicKeyToken=null
or one of its dependencies. An attempt was made to load a program with an incorrect format.
How can I fix this?
Update
I want to be able to keep the main project as any cpu.
Thanks.
On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL).
64-bit EXEs cannot load 32-bit DLLs. (And vice versa: 32-bit EXEs cannot load 64-bit DLLs.)
You'll need to build your .NET project as 32bit (x86 target) if you want it to correctly load a 32-bit DLL on a 64bit machine.
RE: Update:
If you want to keep your project as "Any CPU", you'll need a 32bit and a 64bit version of the DLL, and make sure the appropriate version is distributed with your app. If you can't build the other project as 64bit, you must build your .NET project as 32-bit only.
You will have to force your EXE project to run in 32-bit mode so it can use that C++ DLL. Project + Properties, Build tab, Platform Target = x86.
You may want to take a look at this article it explains why it is not possible, in short since you are dealing with pointers when accessing unmanaged code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With