Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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.
0
d4jaj1
Asked:
d4jaj1
  • 3
  • 3
1 Solution
 
pjdbCommented:
1) You need to create a procedure in the DLL, say ShowMyFrom, which appear in the exports clause of the DLL. This procedure should look like (if we suppose that your form is named MyForm)

Procedure ShowMyFrom;
Begin
  MyForm:=TMyForm.Create(nil); {create the form, some what identical to the createform}
  MyForm.ShowModal; {show the form a modal}
  MyForm.Free; {free the memory allocated during the creation}
End;

All the methods needing for the form must be in the DLL.

2) The forms inside the DLL can pass any data just like if the were in the same exe. However, exchanging data with the exe can be a little bit more complex because you need to define structures to share memory. If you need more information about it, let me know

JDB
0
 
d4jaj1Author Commented:
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;

0
 
pjdbCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
d4jaj1Author Commented:
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?
0
 
pjdbCommented:
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
0
 
d4jaj1Author Commented:
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now