Calling a 64 bit library from a Delphi 32 bit process
Since we are still waiting for a Delphi 64 bit compiler, the only available solution to access a 64 bit library from an application written in Object pascal, is to use the 64 bit version of the FreePascal Compiler.
But you just can not recompile your VCL/GUI based Delphi application with FPC:
- Some low-level part of your code may not be directly compatible with a 64 bit process (e.g. since the pointer size changed);
- The GUI part of the application can not be ported directly with FPC - the Lazarus project try to be as close as possible to VCL, but it can be a very difficult, either impossible if you use some third-party components.
It's a complex solution, running an asm-written 64 bit executable in the background, then communicating with it using memory mapped files and windows GDI messages to launch some 64 bit functions from a 32 bit Delphi process.
There is a sample which is able to load any 64 bit library, then call any function of this library, from a 32 bit executable.
Here are the typical steps of this solution:
- Delphi side creates a memory mapped file, either on disk, either on memory;
- It creates a mutex to notify file change;
- It lauchs a small 64 bit executable process, written in plain assembler (less than 5 KB in size!);
- Delphi client writes some data to the memory mapped file, including the function name and its parameters;
- Then it notifies the 64 bit background process via a GDI message;
- The 64 bit process receives the notification;
- Then it reads the data from the memory mapped file, and unserialize all parameters (including memory buffers);
- Then calls the 64 bit library;
- It serializes the answer into the memory mapped file;
- Then notify the Delphi client via a mutex;
- Delphi client unserialize the answer;
- Delphi 32 bit application can use the incoming data.
This solution seems to work well.
In all cases, code is worth looking at it: it's technical, but well designed!
Link is available at http://cc.embarcadero.com/Author/802978.