Solved

Newbie - DLL Usage

Posted on 1998-05-08
6
219 Views
Last Modified: 2010-04-06
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
Comment
Question by:d4jaj1
  • 3
  • 3
6 Comments
 
LVL 1

Accepted Solution

by:
pjdb earned 50 total points
ID: 1343485
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
 
LVL 3

Author Comment

by:d4jaj1
ID: 1343486
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
 
LVL 1

Expert Comment

by:pjdb
ID: 1343487
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:d4jaj1
ID: 1343488
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
 
LVL 1

Expert Comment

by:pjdb
ID: 1343489
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
 
LVL 3

Author Comment

by:d4jaj1
ID: 1343490
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now