引用了以前人家发的一个帖子你可以参照一下:
一、用Delphi创建DLL
Delphi的DLL创建并不复杂,下面向大家介绍Delphi的DLL创建方法。
1、首先创建一个新的DLL项目(NewProject),因为DLL与调用它的主程序要分开编译。如果DLL模块已经建立在调用它的项目中(Project),则将它的窗体
(Form)从Project移出,另建一个新的项目(NewProject)。只需从File菜单选中NewProject项,然后将DLL的Pas模块文件加入到项目中,再将其自动建立的
Form1删除即可。
2、在DLL的DPR文件中把Program关键字改为Library,申明为动态链接库,在USES语句后面加入ExPorts语句,指明调用DLL的函数名。
3、如果主程序的DPR文件已有DLL窗体CreateForm的语句,则将其去掉。
4、在DLL的Pas文件中Type......End后加入该DLL的函数或过程的声明,形式如:
FunctionName(argment):Boolean;export;
该函数或过程应加入窗体的Create和Free(产生和释放)方法。
5、对项目进行编译即可。
二、DLL的调用
调用DLL有两种方法,一种是在应用程序装载时调用,另一种是在应用程序运行时调用。首先介绍装载时DLL的调用:
(1)装载时调用DLL
在调用DLL的Pas文件中,对DLL函数进行外部声明,声明应位于Implementation的Uses语句后,形式如下:
Implementation
Uses Dialogs;
Function Name(argment):Boolean;far;External 'Call?Name';
......
其中External关键字后面的引号内的字串是DLL的文件名。声明以后即可在Pas文件任何地方引用DLL函数。
装载时调用DLL的优点是速度较快,程序间也可共享代码。
(2)运行时调用DLL
DLL的另一种调用方法是在运行时调用。要调用到Windows的API函数:LoadLibrary,GetProcAddress等。主要用于调用
DELPHI和其它语言,特别是C++编译的DLL。
假定你的DLL包括一个函数:
Function MyFunc(aparam:word):string;export;
首先在程序Type类型声明处加入一句:
Type
TMyfunc = function(aparam:word):string;
此句的作用如同C++中声明的函数指针。
然后定义如下变量∶
Var
aptr:TFarproc;
lhnd:THandle;
s:string;
其中Aptr,lhnd两变量声明必须有,s是DLL函数返回值,视情况而定。
在调用DLL处加入如下语句进行DLL装载:
lhnd:=Loadlibrary('路径
LL文件名');{如lhnd:=Loadlibrary('c:/aa/bb.dll');
aptr:=GetprocAddress(lhnd,'Myfunc');
下面可直接调用DLL了:
s:=TMyfunc(bptr)(60);{根据函数填相应的变量参数}
调用完以后,用FreeLibrary释放DLL占用的内存:
FreeLibrary(lhnd);
下面给出一个DLL的创建以及运行时调用的示例,该DLL主要用来检查输入的口令是否正确,窗体含有一个Edit
编辑框,两个按钮Button,一个标签Label,在编辑框内输入口令,根据比较结果返回真假值。
{main.pas主程序(运行时调用DLL)}
unitMain;
interface
uses WinTypes,WinProcs,Classes,Graphics,Forms,Controls,StdCtrls,ExtCtrls;
type
TForm1= class(TForm)
Edit1:TEdit;
Label1:TLabel;
Button1:TButton;
Bevel1:TBevel;
GroupBox1:TGroupBox;
StatusLbl:TLabel;
procedure Button1Click(Sender:TObject);
end;
TGetPass=function(aa:string):boolean; //函数声明
var //全局变量声明
Form1:TForm1;
getpass:TGetpass;
lhnd:THandle;
aptr:TFarProc;
implementation
uses Dialogs;
{$R*.DFM}
{Import routine from DLL.Takes password to match and returns boolean .}
{function GetPassword(Password:string):Boolean;far;external'CHKPWORD';}
{Call password check routine,show status information.}
procedure TForm1.Button1Click(Sender:TObject);
begin
if Edit1.Text = ''then
begin
MessageDlg('Enter sample password first',mtInformation,[mbOK],0);
Edit1.SetFocus;
end
else begin
lhnd := LoadLibrary('Chkpword.dll'); //运行的时候调用Chkpword.DLL
aptr:= GetProcAddress(lhnd,'GetPassword'); //DLLZ的函数名称为GetPassword
if TGetPass(aptr)(Edit1.Text) then //对DLL进行调用
StatusLbl.Caption := 'Verified password'
else
StatusLbl.Caption := 'Invalid password';
freelibrary(lhnd); //释放
end;
end.
{dllform.pasDLL模块}
unit Dllform;
interface
uses WinTypes,WinProcs,Classes,Graphics,Forms,Controls,Buttons,SysUtils,StdCtrls;
type
TPasswordForm = class(TForm)
Edit1:TEdit;
Label1:TLabel;
BitBtn2:TBitBtn;
BitBtn1:TBitBtn;
end;
function GetPassword(Password:string):Boolean;export;
implementation
uses Dialogs;
{$R*.DFM}
functionGetPassword(Password:string):Boolean;
var
PasswordForm:TPasswordForm;
begin
Result:=False;
PasswordForm:=TPasswordForm.Create(Application);
try
with PasswordForm do
if ShowModal=mrOK then
if UpperCase(Edit1.Text)<> UpperCase(Password)then
MessageDlg('InvalidPassword',mtWarning,[mbOK],0)
else
Result:=True;
finally
PasswordForm.Free;
end;
end;
end.