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

安稳与你 提交于 2019-12-29 07:32:06

问题


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?


回答1:


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.)




回答2:


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.




回答3:


I had the same issue some time back and found this link:32-bit DLLs in 64-bit environment

The 32-bit DLL was written in Delphi, ages ago, and we now had a need to call it from a 64-bit platform- but we don't have a 64-bit Delphi.

I've made it work- though it seems a bit of a kludge, it was better than getting the DLL rewritten in 64-bit (we'd have had to purchase a 64-bit version of Delphi, or start from scratch in something else).

NB while this needs some hacking, no programming is required- it uses components that came with Windows. Works in (at least) Windows 7, Windows 2008.



来源:https://stackoverflow.com/questions/8484204/how-can-i-call-a-32-bit-dll-from-64-bit-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!