关于动态调用dll的问题

hstod 2002-11-26 04:46:55
这是我的dll

library HDB;

uses ComCtrls;

procedure Look(Alistview:TlistView);stdcall;
var
i:integer;
Alist:Tlistitem;
begin
for i:=1 to 50 do
begin
Alist:=Alistview.Items.Add;
Alist.Caption:=inttostr(i);
end;
end;

exprots
look;

begin
end.
我在程序中调用
procedure TForm1.Button3Click(Sender: TObject);
var
Ahandle: Thandle;
AProc: procedure (Alistview:TlistView);stdcall;
begin
Ahandle:=loadlibrary('c:\HDB.dll');
if Ahandle<=0 then
showmessage('error1')
else begin
@Aproc:=getprocaddress(Ahandle,'Look');
if not assigned(@Aproc) then
showmessage('error2')
else
Aproc(listview1);
end;
Freelibrary(Ahandle);
end;
为什么每次进行提示错误!!!!好象就是 FreeLibrary(Ahandle)上!!
...全文
39 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hstod 2002-11-27
  • 打赏
  • 举报
回复
library HDB;

uses ComCtrls;

procedure Look(Alistview:TlistView);stdcall;
var
i:integer;
Alist:Tlistitem;
begin
Alist:=Tlistitem.Create(nil); [这点我 Alistview.items 和 application] 都试过了
for i:=1 to 50 do
begin
Alist:=Alistview.Items.Add;
Alist.Caption:=inttostr(i);
end;
Alist.free;
end;

exprots
look;

begin
end.
我在程序中调用
procedure TForm1.Button3Click(Sender: TObject);
var
Ahandle: Thandle;
AProc: procedure (Alistview:TlistView);stdcall;
begin
Ahandle:=loadlibrary('c:\HDB.dll');
if Ahandle<=0 then
showmessage('error1')
else begin
@Aproc:=getprocaddress(Ahandle,'Look');
if not assigned(@Aproc) then
showmessage('error2')
else
Aproc(listview1);
Freelibrary(Ahandle); [改为 //Freelibrary(Ahandle);]
end;
end;
我将程序改后 还是出错 如果不释放 则不会错 到底释放不释放呀
给一个明确人答案吧
findcsdn 2002-11-27
  • 打赏
  • 举报
回复
楼上说的对
if not assigned(Aproc) then //不要加@
zhang21cnboy 2002-11-27
  • 打赏
  • 举报
回复
当然需要释放了!你的问题出在一个很小的地方,看看吧你exports look

然后@Aproc:=getprocaddress(Ahandle,'Look');的话,能得到嘛?这个地方就错了!if not assigned(@Aproc) then
showmessage('error2')
这样判断@Aproc是否是nil是不正确的!应该是if not (@Aproc=nil)

还有,建议使用try finally结构!
董董 2002-11-26
  • 打赏
  • 举报
回复
刚才我看错了,不好意思:)


问题可能还是出在AList上面。因AList被申明为过程中的局部变量,过程结束时,它的资源应该被释放。listview1中不能够保存过程体产生中的局部变量。

另外,不管Ahandle是否成功获得DLL句柄,程序都会执行Freelibrary(Ahandle)。如果DLL本身就没有被成功加载,则释放它时,肯定会出问题。
hstod 2002-11-26
  • 打赏
  • 举报
回复
你说是要先创建了
董董 2002-11-26
  • 打赏
  • 举报
回复
错误很明显嘛!

procedure Look(Alistview:TlistView);stdcall;
var
i:integer;
Alist:Tlistitem; <====
begin
for i:=1 to 50 do
begin
Alist:=Alistview.Items.Add;
Alist.Caption:=inttostr(i);
end;
end;

Alist只是被申明,但在过程体中,却并没有被建立。直接使用未建立的对象,当然有错啦。
hstod 2002-11-26
  • 打赏
  • 举报
回复
请给出解决方案

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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