ايجاد يک DLL
با استفاده از منو فايل گزينه New Items را انتخاب کنيد و آيتم DLL Wizard را انتخاب نماييد. حال به فايل ايجاد شده، يک فرم با استفاده از روش بالا اضافه نماييد. دقت نماييد که Application را بجاي فرم انتخاب ننماييد. حال اگر فرض کنيم که نام فرم شما Demo باشد و بانام UDemo.pas آنرا ذخيره کرده باشيد. بايد در فايل DLL بصورت زير کد نويسي نماييد:



کد:
 
 
library demodll; { 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, UDemo in "UDemo.pas" {Demo}; {$R *.res} procedure ShowdemoForm;stdcall; begin Demo :=Tdemo.Create(nil); demo.Show; end; function ShowdemoFormModal:integer;stdcall; begin demo :=Tdemo.Create(nil); Result := demo.ShowModal; end; Exports ShowDemoForm, ShowdemoFormModal; begin end.





دقت کنيد که نام DLL فوق DemoDll مي باشد و با نام DemoDll.dpr ذخيره گرديده است.

حال بر روي فرم موجود تمام دکمه‌ها و آبجکت‌هاي مورد نظرتان را اضافه و کد نويسي کنيد (اختياري). در پايان در منو Project گذينه Build DemoDll را انتخاب کرده و اجرا نماييد. فايلي با نام DemoDll.dll ايجاد مي گردد که براي استفاده آماده است.


استفاده از يک DLL بصورت ديناميکي
براي استفاده از يک DLL ‌بصورت ديناميکي، ابتدا نام توابعي را که در فايل DLL شما موجود است بصورت زير تعريف نماييد:

کد:
 


unit UMain; interface uses windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TShowdemoFormModal= Function :integer; . . .






دقت کنيد که نام برنامه انتخابي پيش فرض Main و با نام UMain.pas ذخيره گشته است. حال براي لود کردن DLL يادشده، يک دکمه بر روي فرم قرارداده آنرا بصورت زير کد نويسي کنيد:

کد:
 


var hndDLLHandle:THandle; ShowdemoFormModal:TShowdemoFormModal; procedure TFMain.Button1Click(Sender: T); begin try hndDLLHandle:=LoadLibrary("Demodll.dll"); if hndDLLHandle <> 0 then begin @ShowdemoFormModal:=getProcAddress(hndDLLHandle,"ShowdemoFormModal"); if addr(ShowdemoFormModal) <> nil then begin ShowdemoFormModal; end else showmessage ("function not exists ..."); end else showMessage("Dll Not Found!"); finally freelibrary(hndDLLHandle); end; end;



فرم شما آماده اجراست. در پايان متذکر مي شوم که استفاده ار روش ديناميکي در لود کردن DLL ها باعث پايين آمدن سرعت نمايش فرم‌ها و در عوض بالارفتن سرعت برنامه خواهد شد.