Use getmemorymanager and use the memory manager in the DLL. Share the exe memory manager with the executable. No need for an extra memory manager since you are using an already existing one.
Each module has it's own memory manager. That is why ansistrings can not be exported from functions without sharing memory. To get ansistrings working in a dll, the trick is to export the memory manager from the dll first through a procedure and a buffer parameter which holds the memory manager for the exe to hook up with.
With delphi, this trick means you do not use borlandsharemem borlandmm or sharemem of any sort, to use ansi strings in dll's.
On freepascal it means no need to use CMEM.pp.
Code came from Trustmaster:
// psplib.pp
{$H+}
{$MODE OBJFPC}
{$SMARTLINK OFF}
library psplib;
{ ... }
procedure GetMemMan (out MemMan : TMemoryManager); stdcall; export;
begin
GetMemoryManager (MemMan)
end;
{ ... }
exports
{ ... }
GetMemMan name 'GetSharedMemoryManager';
{ ... }
end.
// pspunit.pp
{$H+}
{$MODE OBJFPC}
{$SMARTLINK OFF}
unit pspunit;
interface
{ ... }
implementation
{ ... }
procedure GetMemMan (out MemMan : TMemoryManager); stdcall; external 'psplib'
name 'GetSharedMemoryManager';
var MemMan : TMemoryManager;
{ ... }
initialization
GetMemMan(MemMan);
SetMemoryManager (MemMan);
{ ... }
end.
// pspapp.pp
{$H+}
{$MODE OBJFPC}
program pspapp;
uses pspunit;
{ ... }
begin
{ ... }
end.
|