Link to home
Start Free TrialLog in
Avatar of d4jaj1
d4jaj1

asked on

Newbie - DLL Usage

Hello,

I just created a DLL is Delphi 2.  The DLL consists of two forms which were previously part of the exe file, but I pulled them out so that another one of my Delphi apps can use them.  So, I have two general questions.

1)  How do I call the DLL from the main exe?  I see the import/export stuff in the Delphi help files, but I'm a little confused.  I know I want the DLL present to start the exe (explicit I think its called), but I'm confused because each of the examples in the help file reference calling a procedure in a DLL (procedure ImportByName; external 'testlib.dll';).  I just want to open a form in the DLL and call methods in the DLL form, from that form.  How do I just display a form stored in the DLL (is there an equivelent to createform)?

2)  Can I assume that once the form in the DLL is displayed, I can use it as if it was part of the main exe?  The first form in the DLL calls the second form in the DLL, which has DB controls in it (query by example).  Please let me know if there are any limitations to passing strings for the QBE from one DLL form to another form in the same DLL OR if I hav eto do anything special to have DB contols in a DLL.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of pjdb
pjdb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of d4jaj1
d4jaj1

ASKER

Thanks,

Yhe DLL compiles okay but there are two problems, one with the window display, the other with Class TDatasource.  Both of teh errors below arise from procedures within the DLL (e.g., they're not passing info back & forth between the exe)

The first problem is with calling the 2nd form (results) from the first one (criteria). For some reason, I can't cal the second form as MODAL.  I get an error of "Cannot make a visible window modal".  Furthermore, the results form appears as it's own application in the TaskList (Alt+Tab), so teh user can switch between the critira & results forms at the same time.  In the exe version, this process appeared to be part of teh same program, e.g., only one tasklist item.  I've tried both Application.CreateForm(TfrmResults, frmResults); and      frmresults := Tfrmresults.Create(nil); - both getting the same modal error.

Secondly, the results form contains an option to create a table and print a report (QuickReport 2).  When I execute the procedure to create teh table - then print the report " Class TDataSource not found" displays.  This is the sam ecode that was in teh exe and it worked fine there.  Not sure where the error occurs, but the tables are created correctly.  Might their be quickreport limitations in a DLL?  Everything seems fine at design time. Here's excerpts from the code if you need it,

if fileexists(extractfilepath(application.exename) + 'Temp.db') then
 begin
  with Table1 do
  begin
   ....open & empty table
  end;
 end
else
 begin
 with Table1 do
  begin
   .....create table
  end;
 end;

try
 begin
  table1.active := true;
  for I := 0 to DstList.Items.Count - 1 do
   begin
   table1.insert;
   table1ingredient.Value := dstlist.Items.Strings[i];
   table1.next;
   end;
  end;

// create quickreport
  try
   begin
    Application.CreateForm(TfrmGListrpt, frmGListrpt);
    frmGlistRpt.GListReport.print;
    label3.visible := true;
   end
  finally
   frmglistRpt.free;
  end;
except
  raise;
end;

For the first problem (form displaying) try  "frmresults := Tfrmresults.Create(application);" instead of  "frmresults := Tfrmresults.Create(nil);" (you need to have the forms unit inthe uses clause)

I can't answer to the second question since i've never used quickreport. Sorry

JDB
Avatar of d4jaj1

ASKER

Okay, don't wrry about the quickreport issue, I'll ask them directly. I'm still having a problem with the show modal issue.  I get the same error (Cannot make a visible window modal).  What does this mean exactly?
There some trouble in your code because i've try on my computer and it work fine! Here the code i've used. Yours should have the same plus the code you need to manage your datas.

an "application" (project1) to make the DLL. Two forms (form1 and form2) in it, a button (button1) on form1 to show the second form modally.

In the file project1.dpr i've the folowing code :
Library Project1;

uses
      Windows,
      Unit1 in 'Unit1.pas' {Form1},
      Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

Procedure ShowForm;      StdCall;
{Show the first form}
begin
      Form1.Show;
end;

exports
      ShowForm;

Procedure DLLEntryPt(reason:integer);
{Manage the creation/freeing of the form when linking/unlinking to the DLL}
Begin
      case reason of
      dll_process_detach, dll_thread_detach:      {0, 3}
            begin
                  Form1.free;
                  Form2.free;
            end;
      dll_process_attach, dll_thread_attach:      {1, 2}
            begin
                  Form1:=TForm1.Create(Application);
                  Form2:=TForm2.Create(Application);
            end;
      end;
End;

{begining of the DLL}
begin
      DLLEntryPt(1);
      DLLProc:=@DLLEntryPt;
end.

in the file unit1.pas :

unit Unit1;

interface

uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;

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

var
      Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
      Form2.ShowModal;
end;

end.

Nothing except the declaration of form2 if unit2.pas

And an application (project2) with one form (form3) with a button (button1) to show the first form (form1) of the DLL.

In the file unit3.pas i've the following :

unit Unit3;

interface

uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;

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

var
      Form3: TForm3;

implementation

{$R *.DFM}

procedure ShowForm; external 'Project1.dll';

procedure TForm3.Button1Click(Sender: TObject);
begin
      ShowForm;
end;

end.

JDB
Avatar of d4jaj1

ASKER

Thanks