一个简单地DLL例子:
//Dll文件
Library MyDll
function max(a,b:integer):integer;stdcall;
begin
if a>b then
result:=a
else
result:=b;
end;
function min(a,b:integer):integer;stdcall;
begin
if a>b
then result:=b
else result:=a;
end;
exports max,min;
//调用DLL之一:动态调用
type
Tmax= function (x,y:integer):Integer;stdcall;
Tmin= function (x,y:integer):Integer;stdcall;
procedure TForm2.Button1Click(Sender: TObject);
var
i,j,k:integer;
hd:thandle;
imax:Tmax;//通过类来使用
imin:Tmin;
begin
hd:=LoadLibrary('Mydll.dll');
i:=strtoint(edit1.Text);
j:=strtoint(edit2.Text);
if (Sender as TButton).Caption='Button1' then
begin
imax:=GetProcAddress(hd,'max');
k:=imax(i,j);
end
else
begin
imin:=GetProcAddress(hd,'min');
k:=imin(i,j);
end;
Edit3.Text:=IntToStr(k);
FreeLibrary(hd);
end;
//静态调用
implementation
function max(a,b:integer):integer;stdcall;external 'Mydll';
function min(a,b:integer):integer;stdcall;external 'Mydll';
var
x,y,z:integer;
begin
x:=strtoint(edit1.text);
y:=strtoint(edit2.Text);
z:=max(x,y);
edit3.Text:=inttostr(z);
end;
Library MyDll
function max(a,b:integer):integer;stdcall;
begin
if a>b then
result:=a
else
result:=b;
end;
function min(a,b:integer):integer;stdcall;
begin
if a>b
then result:=b
else result:=a;
end;
exports max,min;
//调用DLL之一:动态调用
type
Tmax= function (x,y:integer):Integer;stdcall;
Tmin= function (x,y:integer):Integer;stdcall;
procedure TForm2.Button1Click(Sender: TObject);
var
i,j,k:integer;
hd:thandle;
imax:Tmax;//通过类来使用
imin:Tmin;
begin
hd:=LoadLibrary('Mydll.dll');
i:=strtoint(edit1.Text);
j:=strtoint(edit2.Text);
if (Sender as TButton).Caption='Button1' then
begin
imax:=GetProcAddress(hd,'max');
k:=imax(i,j);
end
else
begin
imin:=GetProcAddress(hd,'min');
k:=imin(i,j);
end;
Edit3.Text:=IntToStr(k);
FreeLibrary(hd);
end;
//静态调用
implementation
function max(a,b:integer):integer;stdcall;external 'Mydll';
function min(a,b:integer):integer;stdcall;external 'Mydll';
var
x,y,z:integer;
begin
x:=strtoint(edit1.text);
y:=strtoint(edit2.Text);
z:=max(x,y);
edit3.Text:=inttostr(z);
end;