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

后端 未结 3 1072
感情败类
感情败类 2020-12-19 03:27

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?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 04:13

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

提交回复
热议问题