请问一个关于调用dll的问题,很让我头疼(100分)

W

wrxljf

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个VC写的DLL文件,文件原型为: char * ATOB (char *a)<br>在delphi中,我怎么也调不成功.原因何在?代码示下:<br>unit Unit2;<br>interface<br><br>&nbsp; &nbsp;uses<br>&nbsp; &nbsp; &nbsp; windows,SysUtils;<br>&nbsp; &nbsp;function atob1 (clpa:char):pchar;stdcall;<br>&nbsp; &nbsp;type<br>&nbsp; &nbsp;Tatob=function (I:char):pchar;stdcall;<br>&nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp;cLpath:string;<br>&nbsp; &nbsp; &nbsp;cLstr:string;<br>&nbsp; &nbsp; &nbsp;atob:Tatob;<br>&nbsp; &nbsp; &nbsp;dllhandle:thandle;<br>implementation<br>&nbsp;function atob1 (clpa:char):pchar;stdcall;<br>&nbsp; &nbsp; var<br>&nbsp; &nbsp; clar:ansistring;<br>&nbsp; &nbsp; cLst:pchar;<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;GetMem(clst,8);<br>&nbsp; &nbsp; &nbsp; &nbsp;clar:=' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;';<br>&nbsp; &nbsp; &nbsp; &nbsp;//clst:=Pchar(clst);<br>&nbsp; &nbsp; &nbsp; &nbsp;if dllhandle=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clpath:=clpa;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dllhandle:=LoadLibrary('C:/ATOB.dll');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; @atob:=GetProcAddress(dllhandle,'ATOB');<br>&nbsp; &nbsp; &nbsp; &nbsp; if @atob&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clst:=atob(clpa);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=clst;<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(Clst);<br>&nbsp; &nbsp; end;<br>end.<br>程序在走到 clst:=atob(clpa)时,就报错.<br>我试着把形参部分改成了Pchar也是一样.晕死我了.问题在哪?<br>出错内容:<br>access violation at address 01081044 in module 'ATOB.DLL'. read of address 00000041<br>期待回答.谢谢
 
Tatob=function (var I:pchar):pchar;stdcall;
 
1、DLL返回指针不是一个好的习惯<br>2、你调用atob后,赋值给clst,已经改变了原有的内容,原来申请的内存泄露了<br>3、如VC原型是char * ATOB (char *a),那么对应该是 function ATOB(a: PChar): PChar
 
参数是pchar嘛
 
老子还是第一次看到这样干滴,内存地址冲突,dll调用的例子网上太多,不用写那么多,先来个静态调用,调试要一步一步的来
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
810
import
I
I
回复
0
查看
681
import
I
顶部