Forum > General

Datatypes Interoperability between Delphi and FPC.

(1/3) > >>

BSaidus:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  // Delphi code  var    Lrec: TSomeREC ;   // some record containing a pointer     Llst: TStringList;  begin     ..     Lrec.Adata = Pointer(Llst);   // <== Assign list to pointer      some_function_rec(Lrec);        end;  In Lazarus/FPC here is the code
 
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  // FPC code  var    Llst: TStringList;  begin     // ..     Llst := TStringList(Arec.Adata);   // <== Get the content of the pointer & assign it to Lis      // .. Process then        end;  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.



Thaddy:
Better way is to pass the delphi stringlist text property as a pchar and the use () to set the freepascal text property

BSaidus:

--- Quote from: Thaddy 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

--- End quote ---
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 ??

Thaddy:
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.

Khrys:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Delphiprocedure some_function(AData: PChar); stdcall; external 'fpc_lib.dll';// ...some_function(PChar(Llst.Text));

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// FPC{$mode objfpc}{$H+} procedure some_function(AData: PChar); stdcall;var  Llst: TStringList;begin  Llst := TStringList.Create();  Llst.Text := AData; // Implicit cast to String, which is AnsiString under H+  // ...end;
Edit: Added explicit calling convention

Navigation

[0] Message Index

[#] Next page

Go to full version