DLL应用实例(2种调用方法都有):
unit dll_test;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
Edit3: TEdit;
Button2: TButton;
Button3: TButton;
Edit4: TEdit;
Edit5: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//直接使用外部函数--直接调用实例
function fun_add(p1,p2:integer):integer
stdcall;external 'P_dllexampl.dll';
function fun_dec(p1,p2:integer):integer
stdcall;external 'P_dllexampl.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption:='加';
edit3.Text:=inttostr(fun_add(strtoint(edit1.Text),strtoint(edit2.Text)));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
label1.Caption:='减';
edit3.Text:=inttostr(fun_dec(strtoint(edit1.Text),strtoint(edit2.Text)));
end;
procedure TForm1.Button3Click(Sender: TObject);
//动态使用外部函数实例--动态调用
var
H: HWnd;
p,p2: function(p1,p2:integer):integer
stdcall;
begin
H := LoadLibrary(PChar('P_dllexampl.dll'));
p:=nil;
if H <> 0 then
begin
p := GetProcAddress(H, PChar('fun_add'));
p2 := GetProcAddress(H, PChar('fun_dec'));
if Assigned(p) then
begin
// a1:=p(1,2);
// a2:=p2(1,2);
edit4.Text:=inttostr(p(strtoint(edit1.Text),strtoint(edit2.Text)));
edit5.Text:=inttostr(p2(strtoint(edit1.Text),strtoint(edit2.Text)));
end;
end;
FreeLibrary(H);
end;
end.