Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Delphi How to Create DLL Contains a Form

Author: admin | Category: Form, Tips and Tricks

In this delphi example tutorial i want to show you how we can make a Dynamic Link Library (DLL) application that contain a form. So when we call a function declared in this DLL then it will show a form. In that case we need to create two project: a DLL project and a project to call the DLL function or procedure.

The DLL Project

Creating a DLL file we can go using a DLL Wizard located at File > New > Other menus.
Delphi DLL Wizard
Delphi the gives us a default DLL template like this:

library showformdll;
 
{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
 
uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas' {Form1};
 
{$R *.res}
 
begin
end.

OK Let it like the original template first, know we need to create a form unit. Create a form unit from File > New > Form Menus. Now we have Project1 with Unit1.
Ok now back to the project1. Add this code below to create a procedure to open Form1 in unit1 then Export the procedure.

library showformdll;
 
{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
 
uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas' {Form1};
 
{$R *.res}
 
Procedure ShowForm(Atitle:String);stdcall;
begin
  form1 := TForm1.Create(nil);
  try
    form1.Caption := Atitle;
    Form1.ShowModal;
  except
 
  end;
end;
 
Exports
  ShowForm;
 
begin
end.

ShowForm procedure is a procedure to open Form1. Save the project as "showformdll" than compile it. This will produce "showformdll.dll".

The Caller
After create the DLL than we need to create a small application to call ShowForm procedure declared in showformdll. Just create a simple application from File > New > Application menus then put a button to call the procedure.
DLL Caller Application
Code is as simply like this:

Procedure ShowForm(Atitle:string);stdcall;external 'showformdll.DLL';
 
procedure TForm1.Button1Click(Sender: TObject);
begin
   ShowForm('This is a Form in DLL');
end;

Save this project as "DLLCaller" for example then it will create "DLLCaller.exe" when compiled. Now you can test your application. When Show Form button clicked, then we will see this form.
Form in DLL

You can download the delphi example code here:http://www.mediafire.com/file/tkqdbtanl5z/dll-form.rar

Tags: ,