1. مهمان گرامی، جهت ارسال پست، دانلود و سایر امکانات ویژه کاربران عضو، ثبت نام کنید.
    بستن اطلاعیه

مقاله: استفاده از DLL ها در دلفی

شروع موضوع توسط hector2141 ‏11/9/12 در انجمن Delphi

  1. کاربر ارشد

    تاریخ عضویت:
    ‏6/9/12
    ارسال ها:
    14,318
    تشکر شده:
    2,702
    امتیاز دستاورد:
    0
    حرفه:
    daneshjo
    استفاده از DLLها در دلفی
    ایجاد یک DLL
    با استفاده از منو فایل گزینه New Items را انتخاب کنید و آیتم DLL Wizard را انتخاب نمایید. حال به فایل ایجاد شده، یک فرم با استفاده از روش بالا اضافه نمایید. دقت نمایید که Application را بجای فرم انتخاب ننمایید. حال اگر فرض کنیم که نام فرم شما Demo باشد و بانام UDemo.pas آنرا ذخیره کرده باشید. باید در فایل DLL بصورت زیر کد نویسی نمایید:


    [TABLE]
    [TR]
    [TD="class: gutter"]1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    [/TD]
    [TD="class: code"]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.

    [/TD]
    [/TR]
    [/TABLE]



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

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


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

    [TABLE]
    [TR]
    [TD="class: gutter"]1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [/TD]
    [TD="class: code"]unit UMain;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ExtCtrls;
    type
    TShowdemoFormModal= Function :integer;
    .
    .
    .

    [/TD]
    [/TR]
    [/TABLE]






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

    [TABLE]
    [TR]
    [TD="class: gutter"]1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    [/TD]
    [TD="class: code"]var
    hndDLLHandle:THandle;
    ShowdemoFormModal:TShowdemoFormModal;
    procedure TFMain.Button1Click(Sender: TObject);
    begin
    try
    hndDLLHandle:=LoadLibrary('Demodll.dll');
    if hndDLLHandle <> 0 then begin
    @ShowdemoFormModal:=getProcAddress(hndDLLHandle,'S howdemoFormModal');
    if addr(ShowdemoFormModal) <> nil then begin
    ShowdemoFormModal;
    end
    else
    showmessage ('function not exists ...');
    end
    else
    showMessage('Dll Not Found!');
    finally
    freelibrary(hndDLLHandle);
    end;
    end;

    [/TD]
    [/TR]
    [/TABLE]