TobjectList between exe and DLL in Delphi

2020-02-15 06:51发布

问题:

can somebody confirm me that it is not possible to exchange between an exe and a dLL written in Delphi a pointer that contains a TobjectList?

Class definition shared between DLL and EXE
TCMStack = CLASS(TObject)
PRIVATE
FEquipment: TCMEquipment;         /// equipement with associated constraints
FNbCoils: integer;                  /// coils used
FListeCoils: TCoilsList;            ///coil list associaed with a stack
....

in executable code:

...
/// Transfer business information to optimisation module
/// fOptimisation is a instance of class 
fOptimisation.TransfererDonneesMetiersDansOptimisation(@TStack, LEVEL_OPTIM_1, false);

The content of @TStack in good except the TobjectList

回答1:

You cannot pass Delphi objects across a module boundary unless you are using runtime packages. So, between a DLL and an EXE, it is not possible.

The reason for this restriction is that in order to pass objects across module boundaries you need to share the types between the modules. And that's not possible with a DLL and an EXE. The sharing of types between different modules is the primary functionality of runtime packages.

The documentation explains the limitation like this:

Libraries are significantly more limited than packages in what they can export. Libraries cannot export constants, types, and normal variables. That is, class types defined in a library will not be seen in a program using that library. To export items other than simple procedures and functions, packages are the recommended alternative. Libraries should only be considered when interoperability with other programming is a requirement.

If you must use DLLs then you need to find some other way to interop. One good option is to use interfaces.