How to pass Delphi string to Prism DLL?

We are trying to pass a string from a Delphi native program to a Delphi Prism DLL. We have no problem passing integers, but strings are incompatible in the DLL. We saw Robert Love's code snippet in response to another question, but there is no code for Delphi's own program.

How to pass strings from Delphi to Delphi Prism DLL?

+5
source share
2 answers

The best way is to use WideString.

For several reasons.

  • It is Unicode and works up to D2009
  • Memory management is carried out in ole32.dll, therefore there is no dependence on the Delphi memory manager or CLR GC.
  • .

Oxygene :

type
  Sample = static class
  private
    [UnmanagedExport]
    method StringTest([MarshalAs(UnmanagedType.BStr)]input : String;
                      [MarshalAs(UnmanagedType.BStr)]out output : String);
  end;

implementation

method Sample.StringTest(input : String; out output : String);
begin
  output := input + "ä ~ î 暗";
end;

"MarshalAs" CLR, . Ansi (PAnsiChar), , , , .

Delphi:

procedure StringTest(const input : WideString; out output : WideString);
  stdcall; external 'OxygeneLib';

var
  input, output : WideString;
begin
  input := 'A b c';
  StringTest(input, output);
  Writeln(output);
end.

, , . PChar DLL. , , D7 D2009 ( dev)

+8

Delphi Win32 , .Net, .NET Delphi Win32 .

PChar, . Windows API.

P.S. ; -)

+1

All Articles