如何动态调用DLL?

lyq027 2010-10-14 11:52:12
以下是比较三个数最大值的一个DLL,如何用动态调用?
library max;
uses
SysUtils,
Classes;
{$R *.res}
function max1(a,b,c:integer):integer;stdcall;
var t:integer;
begin
if a>b then t:=a
else t:=b;
if t>c then result:=t
else result:=c;
end;
exports
max1;
begin
end.

主程序有四个框,Edit1,Edit2,Edit3为输入框,Edit4为输出框
...全文
143 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyq027 2010-10-15
  • 打赏
  • 举报
回复
上面的运行,得出的结果不正确呀
yadan_tanchfeng 2010-10-14
  • 打赏
  • 举报
回复
PMax := GetProcAddress(PHandle, 'Max1'); 这里改下
yadan_tanchfeng 2010-10-14
  • 打赏
  • 举报
回复
type
Tmax = function max1(a,b,c:integer):integer;stdcall;
var
PMax: TFarproc;
PHandle : THandle;
A,B,C,D:Double;
DllName : string;
begin
A := StrToFloat(edit1.Text);
B := StrToFloat(edit2.Text);
C := StrToFloat(edit3.Text);

DllName := 'max.dll';
PHandle := LoadLibrary(PChar(DllName));
if PHandle <> 0 then
begin
try
PTest := GetProcAddress(PHandle, 'Max1');
D := Tmax(PMax)(A,B,C);
finally
FreeLibrary(Cardinal(DllName));
end;
end
else
ShowMessage('加载DLL失败!');
end;

差不多就是这样了
FOREST169 2010-10-14
  • 打赏
  • 举报
回复
var
h :thandle;
max1 :function(a,b,c:integer):integer;stdcall;
begin
h := LOADLIBRARY('max.DLL');
@max1 := GETPROCADDRESS( h, 'max1');
edit4.text := inttostr( max1( strtoint(edit1.text),strtoint(edit2.text),strtoint(edit3.text) );
FREELIBRARY( h);
end;
dinoalex 2010-10-14
  • 打赏
  • 举报
回复
判断是否成功读取到地址

应该是这样写

if Assigned(Max1) then
lyq027 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 goodhj 的回复:]
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel……
[/Quote]
if Assigned(@max1) then
请问下,为何此处要设一IF函数,代码什么意思?
goodhj 2010-10-14
  • 打赏
  • 举报
回复
var
max1:Tmax=nil;
改为
var
max1:Tmax;
goodhj 2010-10-14
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Edit4: TEdit;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tmax:function(a,b,c:integer):integer;stdcall;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
max1:Tmax=nil;
PHandle : THandle;
i,j,k,p:integer;
begin
PHandle := LoadLibrary('max.dll');
if PHandle <> 0 then
begin
i := StrToint(edit1.Text);
j := StrToint(edit2.Text);
k := StrToint(edit3.Text);
try
@max1:= GetProcAddress(PHandle,'Max1');
if Assigned(@max1) then
p:= max1(i,j,k);
Edit4.Text:=inttostr(p);
finally
FreeLibrary(PHandle);
end;
end
else
ShowMessage('加载DLL失败!');
end;

end.
max.dll要和你执行文件在同一目录
lyq027 2010-10-14
  • 打赏
  • 举报
回复
ShowMessage('¼ÓÔØDLLʧ°Ü!'); 乱码了

ShowMessage('加载DLL失败!');这才是正确的
lyq027 2010-10-14
  • 打赏
  • 举报
回复
谢谢楼上两位的回复,不过用楼上两位的代码没编译过。我依据你们的思路修改了一下代码,也是不行,麻烦看看哪里出错了,第一次学动态调用,还不是很理解其中原理。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Edit4: TEdit;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var max1:function(a,b,c:integer):integer;stdcall;
PHandle : THandle;
i,j,k,p:integer;
begin
PHandle := LoadLibrary('max.dll');
if PHandle <> 0 then
begin
i := StrToint(edit1.Text);
j := StrToint(edit2.Text);
k := StrToint(edit3.Text);
try
@max1:= GetProcAddress(PHandle,'Max1');
p:= max1(i,j,k);
Edit4.Text:=inttostr(p);
finally
FreeLibrary(PHandle);
end;
end
else
ShowMessage('¼ÓÔØDLLʧ°Ü!');
end;
end.
keeley20 2010-10-14
  • 打赏
  • 举报
回复
LoadLibrary 加载dll
GetProcAddress 获得函数地址
执行函数
Freelibrary 释放dll

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧