Korak 1

U Delphiju 7 (ili D6):
File > New > Other... (Tab: "New") > DLL Wizard

Delphi će kreirati stub fajlove sa dve uključene biblioteke: SysUtils, Classes;
Iznad klauze "uses" primetićete dugo objašnjenje o “memorijskom menadžmentu”. Ovo je važna informacija ako želite da prođete Delphi nizove po vašim metodama kao parametre. Preporučuje se da istražite ovu temu više.

Sad dodajte jedinicu "Forms". Ona vam treba za "THandle". Ona će takođe povećati vaš DLL za dodatnih >300 KB.

Dodajte jedinicu "Windows", takođe.

Sad dodajte formu vašem DLL-u na isti način na koji dodajete forme kad kreirate EXE.

Sačuvajte projekat kao 'forms'.

Korak 2

Možete proglasiti proceduru umesto funkcije, ako ne želite da vaš DLL vrati vrednost. Demonstriraću "modal" and "modeless" oblike kreacije.Testirajte oboje odvojeno.

Dll jedinica bi trebalo da izgleda ovako:

{DLL: 'forms.dll'}
library

uses
Windows,
SysUtils,
Classes,
Forms,
Unit1 in {Unit1.pas};

var
DLLHandle: Longint = 0; { this var will hold the DLL's Handle }

{ "Modal form" }
function DLLMyForm(hHandle: THandle): Integer; stdcall;
var
F: TForm1;
begin
DLLHandle := Application.Handle;
try
Application.Handle := hHandle;
{uvek koristite Application object kao Owner!}
F := Form1.Create(Application);
try
Result := F.ModalResult; {možete koristiti ovu vrednost  host app}
finally
F.Free;
end;
finally
{vraća prethodni handle}
Application.Handle := DLLHandle;
end;
end;

{ "Mode-less form" }
function DLLMyForm(hHandle: THandle): Integer; stdcall;
var
F: TForm1;
begin
if Application.Handle <> hHandle then
DLLHandle := Application.Handle;
Application.Handle := hHandle;
F := TForm1.Create(Application);
F.Show;
Result := Longint(F);
{IMPORTANT: in Form1 OnClose event add "Action := caFree;" to free
allocated resources (Memory leaking) }
end;

exports
{morate izvesti ovaj metod, zar ne? }
DLLMyForm;

begin
end.


Sastavite pomoću Ctrl+F9.

Korak 3

Kreirajte jednostavnu host aplikaciju odakle ćete pozvati vašu DLL formu(e).

Zbog jednostavnosti, koristićemo statični uvoz i jedno dugme da se pozove DLL forma.

U vašem interfejsu proglasite spoljnji metod (ovde funkcija) implementiranim u vaš DLL.

 { Program }
unit Unit1;

interface

uses Windows; { treba nam jedinica Windows za THandle }
...
{metod implementiran u DLL}
function DLLMyForm(hHandle: THandle): Integer; stdcall; external 'forms.dll';
...
implementation

procedure TForm1.Button1Click(Sender: TObject);
var
formvalue: Integer;
begin
{
Pozovite funkciju DLL ; Ona će vratiti Integer vrednost, u ovom slučaju,
modal state (mrOk, mrCancel...). Možete je koristiti za proveravanje validnosti nečega
}
formvalue := DLLMyForm(Handle);
{ nastavite sa rezultatom formvalue ... }
end;

4

Korak 4

Sastavite EXE u isti folder kao i DLL.

Izdvajamo iz ponude: