----------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
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);//調用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
{//靜態調用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------------------------