Recent

Author Topic: Datatypes Interoperability between Delphi and FPC.  (Read 1054 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 606
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Datatypes Interoperability between Delphi and FPC.
« on: December 10, 2024, 09:33:15 am »
Hello.
I'm trying to pass a Delphi(7) TStringList to a dll written in FPC and it do not works perhaps there is an Incompatibilities in the implimentation.
So I tried with pointers,
 In delphi code:
 
Code: Pascal  [Select][+][-]
  1.   // Delphi code
  2.   var
  3.     Lrec: TSomeREC ;   // some record containing a pointer
  4.     Llst: TStringList;
  5.   begin
  6.      ..
  7.      Lrec.Adata = Pointer(Llst);   // <== Assign list to pointer
  8.      some_function_rec(Lrec);      
  9.   end;
  10.  
In Lazarus/FPC here is the code
 
Code: Pascal  [Select][+][-]
  1.   // FPC code
  2.   var
  3.     Llst: TStringList;
  4.   begin
  5.      // ..
  6.      Llst := TStringList(Arec.Adata);   // <== Get the content of the pointer & assign it to Lis
  7.      // .. Process then      
  8.   end;
  9.  
IT simply do not works.
During test, I constat that even with mode delphi, variables type string do not work. I replaced all strings with PChars but for the StringLists, it do not works.

Any solution for this ?
I ask because, I want to write a wrapper around TFPHttpClient to use in delphi, since it can handle different version of openssl, not like indy one.

Thank you.



lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 16387
  • Censorship about opinions does not belong here.
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #1 on: December 10, 2024, 09:48:39 am »
Better way is to pass the delphi stringlist text property as a pchar and the use () to set the freepascal text property
There is nothing wrong with being blunt. At a minimum it is also honest.

BSaidus

  • Hero Member
  • *****
  • Posts: 606
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #2 on: December 10, 2024, 09:59:38 am »
Better way is to pass the delphi stringlist text property as a pchar and the use () to set the freepascal text property
Hi Thaddy.
It is a good idea, but I think that PChar is limited in length to 255 when casting to string.
The StringList I want to use is for TFPHttpClient Post parameters, so what if the combination of all parameter is out of 255 ??
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Thaddy

  • Hero Member
  • *****
  • Posts: 16387
  • Censorship about opinions does not belong here.
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #3 on: December 10, 2024, 10:31:24 am »
Only shortstring has a limit. PChar can be as large as you want: high(ptruint)
There is one big difference with pascal strings: a pchar can not contain #0 values somewhere in the middle, but that is only a problem if you misuse a pchar to represent binary data.
« Last Edit: December 10, 2024, 10:34:42 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Khrys

  • Full Member
  • ***
  • Posts: 128
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #4 on: December 10, 2024, 10:32:12 am »
PChar  is not limited in length, but  ShortString  is (which  String  is an alias for unless  {$H+}  is used), so either add  {$H+}  to your Free Pascal code or explicitly use  AnsiString  when casting.

In any case, when interfacing between toolchains (such as FPC and Delphi) it's best to use simple scalar parameters while avoiding classes and managed types (including  AnsiString), since the latter two aren't generally compatible.

So like Thaddy said it's probably best to use the  Text  property and pass the string as  PChar:

Code: Pascal  [Select][+][-]
  1. // Delphi
  2. procedure some_function(AData: PChar); stdcall; external 'fpc_lib.dll';
  3. // ...
  4. some_function(PChar(Llst.Text));

Code: Pascal  [Select][+][-]
  1. // FPC
  2. {$mode objfpc}{$H+}
  3.  
  4. procedure some_function(AData: PChar); stdcall;
  5. var
  6.   Llst: TStringList;
  7. begin
  8.   Llst := TStringList.Create();
  9.   Llst.Text := AData; // Implicit cast to String, which is AnsiString under H+
  10.   // ...
  11. end;

Edit: Added explicit calling convention
« Last Edit: December 10, 2024, 11:09:29 am by Khrys »

cdbc

  • Hero Member
  • *****
  • Posts: 1779
    • http://www.cdbc.dk
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #5 on: December 10, 2024, 11:00:01 am »
Hi
Quote
I'm trying to pass a Delphi(7) TStringList to a dll written in FPC and it do not works perhaps there is an Incompatibilities in the implimentation.
For that to work, the memory layout behind the scenes, in the Delphi & the Fpc  implementation of TStringList, would have to be exactly (bit for bit) the same!
...and even then I don't think it would work, sometimes even between 2 Fpc compiler(s)/versions, there are small differences...
You might have better luck with COM Interfaces, but that's an exercise for you, the reader to embark on. (and there memory plays a part too)
Stay with the advice of the others.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

CCRDude

  • Hero Member
  • *****
  • Posts: 614
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #6 on: December 10, 2024, 11:12:20 am »
For any DLL, I would go another step and use PAnsiChar or PWideChar/PUnicodeChar, not just PChar. Just to be more clear about the type.

In FreePascal, PChar usually is still PAnsiChar, but I remember the chaos when Delphi switched the default from Ansi to Unicode, and even in FreePascal, if someone uses $mode DelphiUnicode, String suddently is UnicodeString (and PChar maybe PUnicodeChar?).

Plus, for anyone not fluent in FreePascal who might want to write an interface to the DLL in another language, reading PAnsiChar or PUnicodeChar is giving a direct hint at what is to be expected.

BSaidus

  • Hero Member
  • *****
  • Posts: 606
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #7 on: December 10, 2024, 01:31:19 pm »
Thank you for your responses, I will use tstringlist.txt as pchar
thank you
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

silvercoder70

  • Full Member
  • ***
  • Posts: 122
    • Tim Coates
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #8 on: December 10, 2024, 01:32:48 pm »
+1 to what CCRDude said.
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

PascalDragon

  • Hero Member
  • *****
  • Posts: 5815
  • Compiler Developer
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #9 on: December 12, 2024, 09:06:33 pm »
I'm trying to pass a Delphi(7) TStringList to a dll written in FPC and it do not works perhaps there is an Incompatibilities in the implimentation.

Do not mix Delphi and FPC object instances. They are simply different from the layout of the code down to the low level layout of the object and VMT. As other wrote, serialize the information contained in the instance on one side and deserialize on the other.

BSaidus

  • Hero Member
  • *****
  • Posts: 606
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Datatypes Interoperability between Delphi and FPC.
« Reply #10 on: December 12, 2024, 09:36:52 pm »
Thank you for your help friends,
I used the Pchar datatype & it works well.
 ;D
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018