This sample demonstrates how to share memory between a DLL and its client application.
You can find the SimpleShareMem sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples > Delphi > RTL > SimpleShareMem
- Subversion Repository for Delphi: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RADStudio_Rio/Object%20Pascal/RTL/SimpleShareMem/
This sample contains a DLL module and a VCL form application. The DLL exports a method that has a string as a parameter and frees the memory allocated for it.
The VCL application creates a form with one button. When the button is pressed, long strings are created and passed as argument to the function from the DLL, which frees them.
How to Use the Sample
- Navigate to Start | Programs | Embarcadero RAD Studio Rio | Samples and open SimpleShareMem.groupproj.
- Build TestDLL.dll.
- Build and Run MainApp.
This project has one source file, MainAppForm, which contains the class for the main form.
TfMainAppForm, the class for the main form, loads the DLL module and invokes the method exported by it.
- The DLL is loaded with the LoadLibrary function and freed with FreeLibrary. Both functions are Windows calls.
- The GetProcAddress Windows call is used to get the address of the function exported by the DLL.
- System.Assigned is used to check whether the function returned by GetProcAddress exists.
Note: ShareMem or SimpleShareMem must be the first unit in your library's uses clause and your project's uses clause (select Project > View Source) if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL, including those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.