程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> delphi中dll運用的例子

delphi中dll運用的例子

編輯:關於SqlServer

----------dll工程文件testDll.pas----------------
library testDll;

{ 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,
  DLLFrm in 'DLLFrm.pas' {frmDLL};

{$R *.res}

//輸出ShowDLLModalForm,ShowDLLForm,ShowCalendar接口方法,以便外部程序調用
exports
  ShowDLLModalForm, ShowDLLForm,ShowCalendar;
 
begin
end. 

-----------------dll工程文件中的窗體文件DLLFrm.pas----------------
unit DLLFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, Calendar, ComCtrls, StdCtrls, Buttons, ExtCtrls;

type
  TfrmDLL = class(TForm)
    calDLLCalendar: TMonthCalendar;
    Bevel1: TBevel;
    OKBin: TBitBtn;
    CancleBin: TBitBtn;
    procedure calDLLCalendarDblClick(Sender: TObject);
    procedure OKBinClick(Sender: TObject);
    procedure CancleBinClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{聲明要引出的方法}
procedure ShowDLLModalForm(aHandle: THandle); stdcall; //模式顯示窗口
procedure ShowDLLForm(aHandle: THandle); stdcall; //非模式顯示窗口
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;stdcall; //模式顯示窗口,返回選擇調用的日期

implementation

{$R *.dfm}

function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
  DLLF

orm: TfrmDLL;
begin
  Application.Handle := AHandle;
  DLLForm := TfrmDLL.Create(Application); //創建並顯示窗體
 try
  DLLForm.Caption := ACaption; //獲得標題
  DLLForm.calDLLCalendar.date := Date();
  DLLForm.ShowModal; //顯示方式為模式化
  if DLLForm.ModalResult = mrOk then
     Result := DLLForm.calDLLCalendar.Date //返回設定日期
  else  Result := Date();
  finally
    DLLForm.Free; //用完後卸載該窗體
  end;
end;

//模式顯示窗口
procedure ShowDLLModalForm(aHandle: THandle);
begin
  Application.Handle := aHandle; //傳遞應用程序句柄
  with TfrmDLL.Create(Application) do //創建窗體
  begin
    try
      ShowModal; //模式顯示窗體
    finally
      free;
    end;
  end;
end;


//非模式顯示窗口
procedure ShowDLLForm(aHandle: THandle);
begin
  Application.Handle := aHandle; //傳遞應用程序句柄
  with TfrmDLL.Create(application) do //創建窗體
    Show; //非模式顯示窗體
end;
procedure TfrmDLL.calDLLCalendarDblClick(Sender: TObject);
begin
  self.Close;
  ModalResult := mrOk;
end;

procedure TfrmDLL.OKBinClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TfrmDLL.CancleBinClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

end.

--------------------測試單元文件FtestDll.pas,用於調用dll-----------------
unit FtestDll;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
    //動態態引入DLL中的方法: 定義DLL中引出的過程類型
  TShowDLLForm = procedure(aHandle: THandle); stdcall;
  ShowDLLModalForm = procedure(aHandle: THandle); stdcall;
  ShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime;stdcall;

    //靜態引入DLL中的方法
//procedure ShowDLLModalForm(aHandle: THandle); stdcall external '..\5-1\DLLShowForm.dll';
//procedure ShowDLLForm(aHandle: THandle); stdcall exte

rnal '..\5-1\DLLShowForm.dll';
//function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; stdcall external '..\5-1\DLLShowForm.dll';

  TFtest = class(TForm)
    btnShowModal: TButton;
    Button1: TButton;
    getdateEdit: TEdit;
    BtnShow: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnShowModalClick(Sender: TObject);
    procedure BtnShowClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
       //指向加載後DLL句柄
    DLLHandle: THandle;
  public
    { Public declarations }
  end;

var
  Ftest: TFtest;

implementation

{$R *.dfm}

procedure TFtest.FormCreate(Sender: T

Object);
begin
  if DLLHandle = 0 then
  begin
    DLLHandle := LoadLibrary('testDll.dll');
    {如果DLLHandle為0,代表加載DLL失敗}
    if DLLHandle = 0 then
      raise Exception.Create('不能加載DLLShowForm.dll');
  end
  else
    MessageDlg('Library already Loaded', mtWarning, [mbok], 0);
end;

//調用DLL裡輸出的ShowDLLModalForm方法,模式顯示窗口
procedure TFtest.btnShowModalClick(Sender: TObject);
var
  ShowDLLForm: ShowDLLModalForm;
begin
  //ShowDLLModalForm(Application.Handle);  // 靜態調用dll方式
  @ShowDLLForm := GetProcAddress(DLLHandle, 'ShowDLLModalForm');
  if (@ShowDLLForm = nil) then
    RaiseLastWin32Error;
  ShowDLLForm(Application.Handle)
end;

//靜態調用dll方式:調用DLL裡輸出的ShowDLLForm方法,非模式顯示函數
procedure TFtest.BtnShowClick(Sender: TObject);
var
  ShowDLLForm: TShowDLLForm;
begin
  //ShowDLLForm(Application.Handle);   // 靜態調用dll方式:
  @ShowDLLForm := GetProcAddress(DLLHandle, 'ShowDLLForm');
  if (@ShowDLLForm = nil) then

>    RaiseLastWin32Error;
  ShowDLLForm(Application.Handle)
end;

{//靜態調用dll方式
procedure TFtest.Button1Click(Sender: TObject);
var
   getDate: Tdatetime;
begin
  getDate := ShowCalendar(Application.Handle,'模式調用窗體');
  getdateEdit.Text := datetostr(getDate);
end;
}
//動態調用dll中的方法
procedure TFtest.Button1Click(Sender: TObject);
var
  MyShowCalendar: ShowCalendar;
  getDate: Tdatetime;
begin
  @MyShowCalendar := GetProcAddress(DLLHandle, 'ShowCalendar');
  if (@MyShowCalendar = nil) then
    RaiseLastWin32Error;
  getDate := MyShowCalendar(Application.Handle,'test');
  getdateEdit.Text := datetostr(getDate);
end;

{ //單獨調用
procedure TFtest.Button1Click(Sender: TObject);
var
 OneHandle : THandle; //定義一個句柄變量
begin
 OneHandle := LoadLibrary('Clendar.dll'); //動態載入DLL,並返回其句柄
 try
  if OneHandle <> 0 then //如果載入成功則獲取ShowCalendar函數的地址
   @ShowCalendar := GetProcAddress(OneHandle, 'ShowCalendar');
   if not (@ShowCalendar = nil) then
    //如果找到該函數則在主窗體的Label1中顯示DLL窗體中設定的日期
    Label1.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
   else
    RaiseLastWin32Error;
 finally
  FreeLibrary(OneHandle); //調用完畢收回DLL占用的資源
 end;
end;
 }
 
procedure TFtest.FormDestroy(Sender: TObject);
begin
  if not (DLLHandle = 0) then
  begin
    FreeLibrary(DLLHandle);
    DLLHandle := 0;
  end;
end;
end.

----------------------------end------------------------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved