在DELPHI应用程序中使用DLL

来源:互联网 发布:辱妻杀人案 知乎 编辑:程序博客网 时间:2024/04/27 22:13

  1、用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〕对项目进行编译即可。

  2、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包括一个函数:

  FunctionMyFunc(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(′路径/DLL文件名′);

  {如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)}

  unit Main;

  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 matchand returns boolean.}

  {function GetPassword(Password:string):Boolean;far;external′CHKPWORD′;}

  {Call password check routine,show status informcaption.}

  procedure TForm1.Button1Click(Sender:TObject);

  begin

  if Edit1.Text=′′then

  begin

  MessageDlg(′Entersamplepasswordfirst′,mtInformation,[mbOK],0);

  Edit1.SetFocus;

  end

  else

  begin

  lhnd:=loadlibrary(′Chkpword.dll′);

  aptr:=getprocaddress(lhnd,′GetPassword′);

  if TGetpass(aptr)(Edit1.Text) then

  StatusLbl.Caption:=′Verifiedpassword′

  else

  StatusLbl.Caption:=′Invalid password′;

  freelibrary(lhnd);

  end;

  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}

  function GetPassword(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(′Invalid Password′,mtWarning,[mbOK],0)

  else

  Result:=True;

  finally

  PasswordForm.Free;

  end;

  end;

  end.
http://tech.sina.com.cn 2000/12/12 11:40 软件世界 林凌

原创粉丝点击