Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a 32-bit DLL from 64-bit code?

I have some 32-bit DLLs that don't have matched 64-bit DLLs. How can I invoke these DLLs from a 64-bit application written in Delphi XE2?

like image 898
Chau Chee Yang Avatar asked Dec 13 '11 04:12

Chau Chee Yang


People also ask

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

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

How do I run a 32bit program on a 64-bit system?

WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows. This allows for 32-bit (x86) Windows applications to run seamlessly in 64-bit (x64) Windows, as well as for 32-bit (x86) and 32-bit (ARM) Windows applications to run seamlessly in 64-bit (ARM64) Windows.

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

A 64-bit application can not use 32-bit DLLs, as well as a 32-bit application can not use 64-bit DLLs. So you either need to compile your application for 32-bit, or you have to create a 64-bit version of your DLL.


2 Answers

No, you cannot directly do this. A 64 bit process can only execute 64 bit code, and a 32 bit process can only execute 32 bit code.

The trick is to use multiple processes.... (Note this can be done for non visual code, and even for GUI elements, though there can be some small but problematic behaviors for visual elements.)

The most common solution is to wrap the 32 bit dll in an out of process COM server, which you can call across the 64/32 bit barrier. (This goes both ways, you can create a 64 bit out of process COM server and call it from a 32 bit application also.)

Yes, there are other ways to conceive of this, but the most common is to use COM:

  1. Create a new 32 bit out of process COM server that hosts your 32 bit DLL and exposes the needed functionality from the 32 bit dll.
  2. Call this COM server from your 64 bit code

I should add that it is also possible to create the new 32 bit COM server as an in-process COM server, and then configure COM+ to run it. COM+ will run it out of process, and magically run your 32 bit in process COM server out of process, where you can call it from 32 and 64 bit code transparently, as if it was in process. (Note, if the COM server is a GUI control, going out of process may or may not work. The team I work with has done it successfully, but there are complexities -- some of which cannot be surmounted -- related to hooking parent windows and controls that cannot be done across the process boundary.)

like image 77
Jonesome Reinstate Monica Avatar answered Oct 29 '22 14:10

Jonesome Reinstate Monica


You can use the same exact technique used to call 64 bit dlls from 32 bit code.

See http://cc.embarcadero.com/Item/27667

"Just" make the contrary: run a background 32 bit process, the communicate from your 64 bit process with it using a memory mapped buffer.

But this is definitively not an easy task. You'll have to rewrite some asm code. I wrote some article about how it works.

The out-of-process COM option is perhaps the easiest to implement. Or use a more simple IPC - like WM_COPYDATA message or any other mean. But you'll definitively need another 32 bit process to link to the 32 bit libraries.

like image 41
Arnaud Bouchez Avatar answered Oct 29 '22 15:10

Arnaud Bouchez