10.1 DLL 特徵
project 使用之单元是采静态连结(Statically Linked),而DLLs 采动态连结
(Dynamically Linked),故Projectl 中并包含DDLs之拷贝
DLLs 可用任何遵守windows DLL 之开发工具来开发或使用,适合多种开发工具
同时开发环境
10.2 DLL 使用
(1) 藉由名称(循序寻找)
Procedure ImportByName; External 'TestLib.dll';
(2) 藉由重新名称
Procedure ImportByNewName;
External 'TestLib.dll' name 'ImportByName';
(3) 藉由序号(找到程式的最快方法)
Procedure ImportByOrdName;
External 'TestLib.dll' Index 1;
(4) Windows Api 之呼叫
Function MessageBox( Hwnd: Integer ; Text,Caption:Pchr
Flags:Integer):Integer;Stdcall;
External 'User32.dll' Name 'MessageBox';
(5)动态库之输入程式单元
(例-1):Delphi 之开发环境中之 uses windows
(例-2):
unit DateTime;{动态库之输入程式单元}
interface
type
TTimeRec = Record
ss : integer;
mi : Integer;
hh : Integer;
end;
type
TDateRec = Record
yy:Integer;
mm:Integer;
dd:Integer;
end;
Procedure SetTime(Var Time:TTimeRec);
Procedure GetTime(Var Time:TTimeRec);
Procedure SetDate(Var Date:TDateRec);
Procedure GetDate(Var Date:TDateRec);
Implementation
Procedure SetTime; External 'DATETIME' index 1;
Procedure GetTime; External 'DATETIME' index 2;
Procedure SetDate; External 'DATETIME' index 3;
Procedure GetDate; External 'DATETIME' index 4;
end;
------------------------------------------------------
program ShowTime; {呼叫程式}
uses WinCrt , DateTime;
var
Time : TtimeRec;
begin
GetTime(Time);
With Time Do
WriteLn( 'Time is',hh,':',mi,':',ss);
end;
-----------------------------------------------------
(6)DDLs 之动态使用
program ShowTime;
uses WinProcs , WinTypesWinCrt;
type
TTimeRec = Record
ss : integer;
mi : Integer;
hh : Integer;
end;
TGETTime = Procedure( var Time : TTimeRec );
Var
Time : TTimeRec;
Handle : THandle;
GetTime : TGetTime;
Begin
Handle := LoadLibrary('DATETIME.DLL');
if Handle >= 32 then
Begin
@GetTime := GetProcAddress( Handle , 'GETTIME' );
If @GetTime <> nil Then {or If Assigned(GetTime) then }
Begin
GetTime(Time)
With Time do
WriteLn('Time is ',hh,':',mi,':',ss);
end;
FreeLibrary(handle);
end;
end.
10.3 DDLs 之撰写
10.3.1 例子
library minmax;
function Min( X , Y : Integer ) : Integer;
StdCall;
{避免非Delphi之程式呼叫,没有支援 register 呼叫惯例(内定值)}
Begin
If X < Y Then Min := X
else Min := Y;
end;
function Max( X , Y : Integer ) : Integer;StdCall;
Begin
If X > Y Then Max := X
else Max := Y;
end;
exports
Min index 1 name Min Resident;
Max index 2 name Max Resident;
Begin
end.
project 使用之单元是采静态连结(Statically Linked),而DLLs 采动态连结
(Dynamically Linked),故Projectl 中并包含DDLs之拷贝
DLLs 可用任何遵守windows DLL 之开发工具来开发或使用,适合多种开发工具
同时开发环境
10.2 DLL 使用
(1) 藉由名称(循序寻找)
Procedure ImportByName; External 'TestLib.dll';
(2) 藉由重新名称
Procedure ImportByNewName;
External 'TestLib.dll' name 'ImportByName';
(3) 藉由序号(找到程式的最快方法)
Procedure ImportByOrdName;
External 'TestLib.dll' Index 1;
(4) Windows Api 之呼叫
Function MessageBox( Hwnd: Integer ; Text,Caption:Pchr
Flags:Integer):Integer;Stdcall;
External 'User32.dll' Name 'MessageBox';
(5)动态库之输入程式单元
(例-1):Delphi 之开发环境中之 uses windows
(例-2):
unit DateTime;{动态库之输入程式单元}
interface
type
TTimeRec = Record
ss : integer;
mi : Integer;
hh : Integer;
end;
type
TDateRec = Record
yy:Integer;
mm:Integer;
dd:Integer;
end;
Procedure SetTime(Var Time:TTimeRec);
Procedure GetTime(Var Time:TTimeRec);
Procedure SetDate(Var Date:TDateRec);
Procedure GetDate(Var Date:TDateRec);
Implementation
Procedure SetTime; External 'DATETIME' index 1;
Procedure GetTime; External 'DATETIME' index 2;
Procedure SetDate; External 'DATETIME' index 3;
Procedure GetDate; External 'DATETIME' index 4;
end;
------------------------------------------------------
program ShowTime; {呼叫程式}
uses WinCrt , DateTime;
var
Time : TtimeRec;
begin
GetTime(Time);
With Time Do
WriteLn( 'Time is',hh,':',mi,':',ss);
end;
-----------------------------------------------------
(6)DDLs 之动态使用
program ShowTime;
uses WinProcs , WinTypesWinCrt;
type
TTimeRec = Record
ss : integer;
mi : Integer;
hh : Integer;
end;
TGETTime = Procedure( var Time : TTimeRec );
Var
Time : TTimeRec;
Handle : THandle;
GetTime : TGetTime;
Begin
Handle := LoadLibrary('DATETIME.DLL');
if Handle >= 32 then
Begin
@GetTime := GetProcAddress( Handle , 'GETTIME' );
If @GetTime <> nil Then {or If Assigned(GetTime) then }
Begin
GetTime(Time)
With Time do
WriteLn('Time is ',hh,':',mi,':',ss);
end;
FreeLibrary(handle);
end;
end.
10.3 DDLs 之撰写
10.3.1 例子
library minmax;
function Min( X , Y : Integer ) : Integer;
StdCall;
{避免非Delphi之程式呼叫,没有支援 register 呼叫惯例(内定值)}
Begin
If X < Y Then Min := X
else Min := Y;
end;
function Max( X , Y : Integer ) : Integer;StdCall;
Begin
If X > Y Then Max := X
else Max := Y;
end;
exports
Min index 1 name Min Resident;
Max index 2 name Max Resident;
Begin
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货