برترین مقالات کامپیوتر

برترین مقالات کامپیوتر

برترین مقالات کامپیوتر

برترین مقالات کامپیوتر

استفاده از DLL‌ها در دلفی

 

ایجاد یک 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: TObject);
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 ها باعث پایین آمدن سرعت نمایش فرم‌ها و در عوض بالارفتن سرعت برنامه خواهد شد.

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد