Board index » delphi » Getting a TStringList as a return from a DLL procedure

Getting a TStringList as a return from a DLL procedure

I am writing a DLL that needs to be able to return a TStringlist back to the calling
application.  I keep getting a GPF when attempting this.  I have no problem returning a normal
string or integer, but the TStringlist is tricky.  If you can give any help I'd greatly
appreciate it.

Here is a sample of the DLL
___________________________________________________________________________________________
Library Dllslist;

uses
  Forms,
  Dllmain in 'DLLMAIN.PAS' {Form1};

{$R *.RES}
Exports
  ReturnMeStringList;

begin
end.
___________________________________________________________________________________________

unit Dllmain;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  DLLStringList: TStringList;

function ReturnMeStringList: TStringList; export;

implementation

{$R *.DFM}

function ReturnMeStringList: TStringList;
begin

  DLLStringList.Add('Line1');
  DLLStringList.Add('Line2');
  DLLStringList.Add('Line3');
  DLLStringList.Add('Line4');

  Result := DLLStringList;

end;

initialization
  DLLStringList := TStringList.Create;

end.

___________________________________________________________________________________________

Here is the code for the EXE

___________________________________________________________________________________________

program Dlltest;

uses
  Forms,
  Unit1 in 'UNIT1.PAS' {frmMain};

{$R *.RES}

begin
  Application.CreateForm(TfrmMain, frmMain);
  Application.Run;
end.

___________________________________________________________________________________________

unit Unit1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Main;

type
  TfrmMain = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{function ReturnMeStringList: TStringList; far; external 'DLLSLIST';   }

{$R *.DFM}

procedure TfrmMain.Button1Click(Sender: TObject);
begin
memo1.lines := ReturnMeStringList;

end;

end.

___________________________________________________________________________________________

That's all there is to it.  If anyone could help me with this puzzle I would greatly
appreciate it.

Thank you
Kevin Burney
KBur...@ix.netcom.com

 

Re:Getting a TStringList as a return from a DLL procedure


On 30 Oct 1995 21:35:07 GMT, "Kevin D. Burney" <KBur...@ix.netcom.com>
wrote:

Quote
>I am writing a DLL that needs to be able to return a TStringlist back to the calling
>application.  I keep getting a GPF when attempting this.  I have no problem returning a normal
>string or integer, but the TStringlist is tricky.  If you can give any help I'd greatly
>appreciate it.

As a general rule, you cannot do this. The problem is that each module
(DLL or EXE) keeps its own copy of the run-time type information.
Delphi relies on the RTTI for the IS and AS operators. When you assign
a TStrings object to a TStrings property, it calls the Assign method,
which uses the IS operator to test the class of the argument.

Instead of trying to pass a TStrings object to a DLL or return a
TStrings object from a DLL, you can pass strings through a callback
procedure.  The EXE calls a DLL procedure, passing a callback as the
argument. The DLL calls the callback for each string, and the EXE is
responsible for adding the string to its string list.
--
Ray Lischner         (li...@tempest-sw.com)
Tempest Software, Corvallis, Oregon, USA

Other Threads