Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a 32 bit DLL in a 64 bit project?

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 assembly TreeTMHook, 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.

like image 414
Jonathan D Avatar asked Aug 25 '10 12:08

Jonathan D


People also ask

Can a 64 bit application call a 32-bit DLL?

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL).

Can 64 bit python load 32-bit DLL?

64-bit EXEs cannot load 32-bit DLLs. (And vice versa: 32-bit EXEs cannot load 64-bit DLLs.)


3 Answers

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.

like image 97
Bob Avatar answered Oct 12 '22 01:10

Bob


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.

like image 26
Hans Passant Avatar answered Oct 12 '22 01:10

Hans Passant


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.

like image 21
AndersK Avatar answered Oct 12 '22 02:10

AndersK