Exchanging strings (PChar) between a Freepascal compiled DLL and a Delphi compiled EXE

Posted by John Riche on Stack Overflow See other posts from Stack Overflow or by John Riche
Published on 2010-04-14T15:21:13Z Indexed on 2010/04/14 16:43 UTC
Read the original article Hit count: 683

Filed under:
|
|
|
|

After a lot of experimentations, I found a way to exchange PChar from a FreePascal compiled DLL with a Delphi compiled EXE. I'm in charge of both the DLL and EXE source code but one MUST BE in FreePascal and the other one in Delphi. My solution involves the following methods in the DLL:

function GetAString(): PChar;
var aString: string;
begin
  aString := 'My String';
  result := StrAlloc(length(aString) + 1);
  StrPCopy(result, aString); 
end;

procedure FreeString(aString: PChar);
begin
  StrDispose(aString); 
end;

And from the Delphi EXE, to call the GetAString method, I need to Call the GetAString method, save the PChar to an actual Delphi String and call the FreeString method.

Is this the best way of exchanging a string from a FreePascal DLL with a Delphi EXE ? Can I avoid the call to FreeString from Delphi ?

And finally, if that's the correct solution, how will it behave with Delphi 2010 and the WideString by default: do I need to force WidePChar in FreePascal too ?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about freepascal