为了让人能快速的理解 静态调用、动态调用,现在做一个函数封装在一个DLL中,然后在APPLICATION form里面调用这个函数,这个函数处理两个数的和。用代码和图片说话:
代码如下
library Project1;
{ 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;
function incc(a,b:integer):Integer; stdcall; //真正的代码开始;
begin
Result :=a+b;
end;
{$R *.res}
exports //允许调用;
incc;
end.
按Ctrl+f9编译以后会在文件夹下面会产生一个 project1.dll的DLL文件。下面,我们就开始用静态调用和动态调用两种方式调用这个DLL里面的函数。
一:静态调用
新建一个application form 在这个窗体上加两个文本框,取名edt1,edt2 用代码说话。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function incc(a,b:Integer):Integer;stdcall; external ‘Project1.dll‘; //加载这个动态库。
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
aa:Integer;
begin
aa:=incc(StrToInt(edt1.Text),StrToInt(edt2.Text));//开始调用
ShowMessage(IntToStr(aa));//弹出结果。
end;
end.
二:相比静态调用,动态调用占用的资源要小点,哎呀,具体好处我就不说了,现在来看看具体怎么能实现,同样的在建立一个和静态调用的窗体。再用代码说话。
unit Unit11;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Tmyfun = function (a,b:integer):Integer; stdcall;//定义一个函数类型,注意一点过程类型种的参数应该与DLL中用到的方法的参数一致。
procedure TForm1.btn1Click(Sender: TObject);
var
myhandle:THandle;
FPointer:Pointer;
Myfun :Tmyfun;
begin
myhandle:=LoadLibrary(‘C:\Documents and Settings\Administrator\桌面\新建文件夹\Project1.dll‘) ;//加载这个DLL
if myhandle >0 then//加载成功就执行。
try
FPointer :=GetProcAddress(myhandle,PChar(‘incc‘)); //取函数的地址。
if FPointer <>nil then //如果函数存在就调用
begin
Myfun := Tmyfun(FPointer);
showmessage(IntToStr(Myfun(StrToInt(edt1.Text),StrToInt(edt2.Text))));//弹出算出的结果。
end;
except
FreeLibrary(myhandle);
end;
end;
end.
在Delphi中静态调用DLL
调用一个DLL比写一个DLL要容易一些。首先给大家介绍的是静态调用方法,稍后将介绍动态调用方法,并就两种方法做一个比较。同样的,我们先举一个静态调用的例子。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//本行以下代码为我们真正动手写的代码
function TestDll(i:integer):integer;stdcall;
external ’Delphi.dll’;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=IntToStr(TestDll(1));
end;
end.