.wysiwyg { BACKGROUND: #f5f5ff; FONT: 10pt tahoma,tahoma,tahoma; COLOR: #000000 } P { MARGIN: 0px } .inlineimg { VERTICAL-ALIGN: middle } ایجاد یک DLL
با استفاده از منو فایل گزینه New Items را انتخاب کنید و آیتم DLL Wizard را انتخاب نمایید. حال به فایل ایجاد شده، یک فرم با استفاده از روش بالا اضافه نمایید. دقت نمایید که Application را بجای فرم انتخاب ننمایید. حال اگر فرض کنیم که نام فرم شما Demo باشد و بانام UDemo.pas آنرا ذخیره کرده باشید. باید در فایل DLL بصورت زیر کد نویسی نمایید:
کد:
library demodll;{ Important note about DLL memory management: ShareMem must be thefirst unit in your library's USES clause AND your project's (selectProject-View Source) USES clause if your DLL exports any procedures orfunctions that pass strings as parameters or function results. Thisapplies to all strings passed to and from your DLL--even those thatare nested in records and classes. ShareMem is the interface unit tothe BORLNDMM.DLL shared memory manager, which must be deployed alongwith your DLL. To avoid using BORLNDMM.DLL, pass string informationusing PChar or ShortString parameters. }usesSysUtils,Classes,UDemo in 'UDemo.pas' {Demo};{$R *.res}procedure ShowdemoForm;stdcall;beginDemo :=Tdemo.Create(nil);demo.Show;end;function ShowdemoFormModal:integer;stdcall;begindemo :=Tdemo.Create(nil);Result := demo.ShowModal;end;ExportsShowDemoForm,ShowdemoFormModal;beginend.
دقت کنید که نام
DLL فوق DemoDll می باشد و با نام DemoDll.dpr ذخیره گردیده است.
حال بر روی فرم موجود تمام دکمهها و آبجکتهای مورد نظرتان را اضافه و کد نویسی کنید (اختیاری). در پایان در منو
Project گذینه Build DemoDll را انتخاب کرده و اجرا نمایید. فایلی با نام DemoDll.dll ایجاد می گردد که برای استفاده آماده است.
استفاده از یک
DLL بصورت دینامیکی
برای استفاده از یک DLL بصورت دینامیکی، ابتدا نام توابعی را که در فایل DLL شما موجود است بصورت زیر تعریف نمایید:
کد:
unit UMain;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTShowdemoFormModal= Function :integer;...
دقت کنید که نام برنامه انتخابی پیش فرض Main و با نام UMain.pas ذخیره گشته است. حال برای لود کردن DLL یادشده، یک دکمه بر روی فرم قرارداده آنرا بصورت زیر کد نویسی کنید:
کد:
varhndDLLHandle:THandle;ShowdemoFormModal:TShowdemoFormModal;procedure TFMain.Button1Click(Sender: TObject);begintryhndDLLHandle:=LoadLibrary('Demodll.dll');if hndDLLHandle <> 0 then begin @ShowdemoFormModal:=getProcAddress(hndDLLHandle,'ShowdemoFormModal');if addr(ShowdemoFormModal) <> nil then beginShowdemoFormModal;endelseshowmessage ('function not exists ...');endelseshowMessage('Dll Not Found!');finallyfreelibrary(hndDLLHandle);end;end;
فرم شما آماده اجراست. در پایان متذکر می شوم که استفاده ار روش دینامیکی در لود کردن DLL ها باعث پایین آمدن سرعت نمایش فرمها و در عوض بالارفتن سرعت برنامه خواهد شد.