下面在delphi中编写一个简单的dll,在该dll中只有一个max函数,返回2个数中的大数(Delphi 5.0)1、New->DLL;取名为DLL_0001,编写代码:library dll_0001;uses
SysUtils,
Classes;{$R *.RES}function max(x,y:integer):integer;stdcall;
begin
if(x>y) then
max :=x
else
max :=y;
end;exports max;beginend.红色部分为自己编写,这里和普通的delphi函数是一样的,只是在返回值中带个stdcall参数,然后用exports把函数导出================================================================================
Delphl调用dll
调用dll分动态调用和静态调用2中,动态调用写起来简单,安全点,动态调用复杂很多,但很灵活;现在编写一个程序,来调用上面写的dll_0001.dll文件中的max函数一、new一个Application,在Form中放入2个TEdit、1个TLabek;二、 1、静态调用unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
function max(x,y:integer):integer;stdcall;
external 'dll_0001.dll';procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key =vk_return then
label1.Caption :=IntToStr(max(StrToInt(Edit1.text),StrToInt(edit2.text)));end;end.红色代码自己添加,其中external "dll_name"中的dll_name可以是dll的绝对路径,要是该dll文件在你的搜索路径中,可以直接写文件名,但是.dll不能少写2、动态调用,代码如下;unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
type
TFunc =function(x,y:integer):integer;stdcall;
var
Th:Thandle;
Tf:TFunc;
Tp:TFarProc;begin
if key =vk_return then
beginTh :=LoadLibrary('dll_0001.dll'); {load dll}
if(Th >0) then
try
Tp :=GetProcAddress(Th,PChar('max'));
if(Tp <>nil) then
begin { begin 1}
Tf :=TFunc(Tp);
Label1.Caption :=IntToStr(
Tf(StrToInt(Edit1.text),StrToInt(Edit2.text)));
end { end 1}
else
ShowMessage('function max not found.');
finally
FreeLibrary(Th);
end
else
ShowMessage('dll_0001.dll not exsit.');
end;
end;end.
SysUtils,
Classes;{$R *.RES}function max(x,y:integer):integer;stdcall;
begin
if(x>y) then
max :=x
else
max :=y;
end;exports max;beginend.红色部分为自己编写,这里和普通的delphi函数是一样的,只是在返回值中带个stdcall参数,然后用exports把函数导出================================================================================
Delphl调用dll
调用dll分动态调用和静态调用2中,动态调用写起来简单,安全点,动态调用复杂很多,但很灵活;现在编写一个程序,来调用上面写的dll_0001.dll文件中的max函数一、new一个Application,在Form中放入2个TEdit、1个TLabek;二、 1、静态调用unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
function max(x,y:integer):integer;stdcall;
external 'dll_0001.dll';procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key =vk_return then
label1.Caption :=IntToStr(max(StrToInt(Edit1.text),StrToInt(edit2.text)));end;end.红色代码自己添加,其中external "dll_name"中的dll_name可以是dll的绝对路径,要是该dll文件在你的搜索路径中,可以直接写文件名,但是.dll不能少写2、动态调用,代码如下;unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
type
TFunc =function(x,y:integer):integer;stdcall;
var
Th:Thandle;
Tf:TFunc;
Tp:TFarProc;begin
if key =vk_return then
beginTh :=LoadLibrary('dll_0001.dll'); {load dll}
if(Th >0) then
try
Tp :=GetProcAddress(Th,PChar('max'));
if(Tp <>nil) then
begin { begin 1}
Tf :=TFunc(Tp);
Label1.Caption :=IntToStr(
Tf(StrToInt(Edit1.text),StrToInt(Edit2.text)));
end { end 1}
else
ShowMessage('function max not found.');
finally
FreeLibrary(Th);
end
else
ShowMessage('dll_0001.dll not exsit.');
end;
end;end.