高分救助dll调用过程中的指针问题!!!(100分)

  • 主题发起人 主题发起人 linqi07
  • 开始时间 开始时间
L

linqi07

Unregistered / Unconfirmed
GUEST, unregistred user!
DLL名:PutGet.dll<br>函数名:PutGet<br>参数1:发送的消息(512位字符串)<br>参数2:返回的消息(512位字符串)<br>估计是c++编写的。dll源型不知道<br>*************<br>delphi中调用如下:<br>//申明<br>&nbsp; &nbsp; function PutGet(sPut,sGet:pchar):pchar; stdcall; External 'PutGet.dll' <br>name 'PutGet';<br>//调用<br>procedure &nbsp;Tform1.Button1Click(Sender: TObject);<br>var &nbsp;sOut,sIn:pchar;<br>&nbsp; &nbsp; &nbsp;ReceiveMsg:string;<br>begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getmem(sout,512);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getmem(sIn,512);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strpcopy(sOut,edit1.text);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendRcv(sOut,sIn);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReceiveMsg:=strpas(sIn);<br>&nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AnalyseReceive(ReceiveMsg); //处理返回的消息串ReceiveMsg<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>end<br><br>运行时偶尔会发生非法指针错误,然后就会出现程序异常。<br>请各位大侠帮我看看以上程序有什么问题???<br>如果问题解决,还会加分。先谢谢各位。<br>
 
去掉try和finally即可
 
zqw0117, <br>不会吧?什么理由呢?
 
由于ReceiveMsg的定义在try块内,而AnalyseReceive(ReceiveMsg); 语句又在finally中,<br>因此可能ReceiveMsg尚未定义完全(如出现错误),程序就会转到finally块执行,当然<br>会出错!
 
不要忘了释放FreeMem<br>procedure &nbsp;Tform1.Button1Click(Sender: TObject);<br>var &nbsp;sOut,sIn:pchar;<br>&nbsp; &nbsp; &nbsp;ReceiveMsg:string;<br>begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getmem(sout,512);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getmem(sIn,512);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strpcopy(sOut,edit1.text);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendRcv(sOut,sIn);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReceiveMsg:=strpas(sIn);<br>&nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AnalyseReceive(ReceiveMsg); //处理返回的消息串ReceiveMsg<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FREEmem(sout,512);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FREEmem(sIn,512);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>end<br><br>
 
zhanggm, <br>&nbsp; 我加过freemem的,但是加了后,每次都会报错。一般是运行第2个freemem时报错的。<br><br>我想是不是函数申明有问题?
 
Linqi07,你的问题解决了吗?如果解决了能告诉我怎么解决的吗?<br>&nbsp;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
890
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
686
import
I
后退
顶部