Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

32 bit dll importing in 64 bit .Net application

I'm having a problem, I've been trying to solve it since yesterday but no luck. I have a 32-bit Delphi DLL which I want to import it in to a .NET WIN Application. This application has to be built on ANY CPU mode. Of course, the exception BadImageFormatException is thrown, which means that 64-bit applications can't load x86 DLLs. I googled around and found a solution, it said that I have to do wrapper, but it wasn't clear for me. Can anyone tell me how to solve this problem, is there any possible way that I can import a 32-bit Delphi DLL in to a program built under any CPU architecture (64-bit, 32-bit) or maybe another solution?

like image 673
scatterbraiin Avatar asked Jun 14 '10 11:06

scatterbraiin


People also ask

Can we use 32-bit DLL in 64-bit application C#?

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

How do I change a 32-bit DLL to 64-bit?

Solution 2 If possible create a separate 32 bits application that uses the 32 bit dll and call that using Process. Start()[^] from your 64 bits application. There are other ways like using named pipes for inter-process communincation, but they are quite complex to implement.

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

Solution. You cannot call a 32-bit DLL from 64-bit LabVIEW, or vice versa. This is a limitation of 64-bit Windows, which does not support mixed 64-bit/32-bit processes. 1.


2 Answers

What you have to do is write a wrapper application that hosts the 32-bit DLL file, in a 32-bit process.

Your 64-bit application then has to talk to this 32-bit process, through network means, or by making the DLL functions available through a COM object, or similar.

You can not run a 32-bit DLL inside a 64-bit process, no matter how hard you try, so you need to run it in a 32-bit process.

If compiling your application for 32-bit only is not an option, you have no choice but to create a host application.

like image 94
Lasse V. Karlsen Avatar answered Oct 25 '22 11:10

Lasse V. Karlsen


A general idea could be to wrap your (unmanaged) 32-bit DLL with a managed 32-bit wrapper dll and make it COM visible. This allows calls to your wrapper DLL via its COM interface.

You can than use a COM surrogate to make your COM dll appear as an out of process COM server. Take a look at this SO question for some further information on this topic: Access x86 COM from x64 .NET.

like image 30
Frank Bollack Avatar answered Oct 25 '22 11:10

Frank Bollack