I am having trouble get a clear picture of the advantages/disadvantages of using Sharemem vs SimpleSharemem to pass strings between a main program and DLLs.
Here is the background for why I am looking into this: My main program is developed in XE2 64-bit (although I may upgrade to XE7) and the DLLs will either be developed with XE2 (or higher) or FPC 64-bit (the ideal scenario). The main program and the DLLs need to be able to pass strings contained in records. Ideally I would like to have the DLLs developed by people who may not have Delphi and it appears as though FPC supports Sharemem but not SimpleSharemem.
Are there performance differences between Sharemem vs SimpleSharemem? Are there other reasons to prefer one vs the other in the scenario I describe (aside from the apparent FPC support for Sharemem)?
Thank you!
Sharemem
is the old way of sharing memory managers. It relies on a DLL being deployed alongside your application.SimpleSharemem
is designed to work with FastMM which handles sharing in a different way fromSharemem
. So for modern versions of Delphi that use FastMM as their memory manager, useSimpleSharemem
.Shared memory managers allow you to allocate memory in one module and deallocate in another, using the standard language heap functions like
GetMem
,New
etc.However, you are attempting rather more than this. You are hoping to share a memory manager between modules compiled by different compilers. Specifically Delphi and FPC. Delphi's
ShareMem
is designed to allow sharing between modules compiled in Delphi. Likewise FPC'sShareMem
is designed to allow two FPC compiled modules to share memory manager. There is no support for sharing between Delphi and FPC modules.Even further, you are hoping to be able to pass string objects between modules. This requires the implementation of the string object to be compatible across the interop boundary. That might be the case for Delphi's
UnicodeString
for XE2 and XE7 modules, but if so it is only by chance. There is no guarantee of that. Future releases of Delphi may change the implementation. And as for mixing Delphi and FPC strings, there's no reason to believe that they can be mixed. I doubt they can.So my advise is to stop trying to use shared memory managers and stop trying to pass native language strings between modules compiled with different languages.
In order to pass text between modules in a mixed compiler environment you need to use valid types for binary interop. Typical approaches include:
PWideChar
. Trivial when passing text in. When passing text out this becomes more difficult because you may need callee to allocate but caller to deallocate. Either export a deallocator or allocate on a shared heap, e.g. the COM heap.WideString
. This type is designed for binary interop.My choice would be to use
WideString
.