Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to really add a form to a DLL Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Jun-03
Category
VCL-Forms
Language
Delphi 2.x
Views
192
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Peter Thörnqvist

Adding a form to a DLL is actually quite simple. Depending on the type of form 
(modal or mode-less) you have to proceed differently. This article explains how it 
is done. 

Answer:

To add a form to a DLL you have to remember these things: 

assign the calling programs Application.Handle to the DLL's Application.Handle 
write one or two exported functions that handles the interaction with the calling 
program 
include sharemem as the first unit in the DLL's uses clause if the exported 
functions uses strings 
if you are going to show a mode-less form, you should return a handle to your form 
in the "Show" function and require it as a parameter in the "Close" function 
always create the form with the Application object as the Owner 
restore the DLL's Application.Handle after closing the form 

You don't have to do anything special to add a form to a DLL: just add the forms 
filename to the uses clause and Delphi will compile it right into the DLL. 

Here's an example with both modal and mode-less invocation. The examples just 
return an integer value, but you could of course return just about anything: 

1   library testDLL;
2   
3   uses
4     myTestFrom, SysUtils, Controls;
5   
6   var
7     OldApphandle: longint = 0;
8   
9     { these functions are used with the mode-less form: }
10  
11    { AppHandle is the *calling* applications Handle }
12  
13  function ShowTestForm(AppHandle: integer): longint;
14  var
15    F: TmyTestForm;
16  begin
17    { save current handle unless it's already done }
18    if Application.Handle <> AppHandle then
19      OldAppHandle := Application.Handle;
20    { assign new }
21    Application.Handle := AppHandle;
22    { create and show form }
23    F := TmyTestForm.Create(Application);
24    F.Show;
25    Result := longint(F);
26  end;
27  
28  { the input value, Handle, must be the same value as returned by ShowTestForm }
29  
30  function CloseTestForm(Handle: longint): integer;
31  var
32    F: TmyTestForm;
33  begin
34    { typecast back to TForm (some sanity checks here would not be bad...}
35    F := TmyTestForm(Handle);
36    Result := F.SomeIntValue;
37    F.Close;
38    F.Free;
39    { restore previous handle }
40    Application.Handle := OldAppHandle;
41  end;
42  
43  { this function is used to show the form modally }
44  
45  function ShowTestFormModal(AppHandle: integer): longint;
46  var
47    F: TmyTestForm;
48  begin
49    OldAppHandle := Application.Handle;
50    try
51      Application.Handle := AppHandle;
52      F := TmyTestForm.Create(Application);
53      try
54        if F.ShowModal = mrOK then
55          Result := F.SomeIntValue
56        else
57          Result := -1;
58      finally
59        F.Free;
60      end;
61    finally
62      Application.Handle := OldAppHandle;
63    end;
64  end;
65  
66  { finally export the functions: }
67  
68  exports ShowTestForm name 'ShowTestForm', CloseTestForm name 'CloseTestForm',
69    ShowTestFormModal name 'ShowTestFormModal';
70  
71  begin
72  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC