怪问题!错在哪了?您能帮我找出问题根源吗? ( 积分: 70 )

  • 主题发起人 主题发起人 taibu
  • 开始时间 开始时间
T

taibu

Unregistered / Unconfirmed
GUEST, unregistred user!
程序设计思路如下:主程序调用一个DLL,DLL再调用消费机接口.<br>DLL中函数如下(ReadICCardEx函数为消费机接口函数):<br>&nbsp;&nbsp;function&nbsp;&nbsp;ICCardReadEx(PortHandle:&nbsp;THandle;Var&nbsp;CardNo,&nbsp;CardName,&nbsp;pwd:&nbsp;PChar;var&nbsp;Money,&nbsp;Times,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day_consumed,&nbsp;day_times,&nbsp;c_month,&nbsp;c_day,&nbsp;c_flag,&nbsp;GroupStation,&nbsp;GroupId,&nbsp;CardStyle:&nbsp;Integer):&nbsp;Boolean;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;CardBuf,&nbsp;NameBuf,PwdBuf:&nbsp;array[1..50]&nbsp;of&nbsp;char;&nbsp;<br>&nbsp;&nbsp;Str1,Str2,Str3:String;<br>begin<br>&nbsp;&nbsp;FillChar(CardBuf,&nbsp;SizeOf(CardBuf),&nbsp;0);<br>&nbsp;&nbsp;FillChar(NameBuf,&nbsp;SizeOf(NameBuf),&nbsp;0);<br>&nbsp;&nbsp;FillChar(PwdBuf,&nbsp;SizeOf(PwdBuf),&nbsp;0);<br>&nbsp;&nbsp;Result:=ReadICCardEx(PortHandle,@CardBuf,&nbsp;@NameBuf,&nbsp;@PwdBuf,Money,Times,Day_Consumed,Day_Times,C_Month,C_Day,C_Flag,GroupStation,GroupId,CardStyle);<br>&nbsp;&nbsp;Str1:=CardBuf;<br>&nbsp;&nbsp;Str2:=NameBuf;<br>&nbsp;&nbsp;Str3:=PwdBuf;<br>&nbsp;&nbsp;CardNo:=Pchar(Str1);<br>&nbsp;&nbsp;CardName:=Pchar(Str2);<br>&nbsp;&nbsp;PWd:=Pchar(Str3);<br>end;<br><br>主程序调用代码如下:<br><br>var<br>&nbsp;&nbsp;DllHandle:THandle;<br>&nbsp;&nbsp;Dllfarproc:Tfarproc;<br>&nbsp;&nbsp;CommuPort:String;<br>&nbsp;&nbsp;Port,BaudRate,Clockid:Integer;<br>&nbsp;&nbsp;RstCon:Integer;<br>&nbsp;&nbsp;DllFile:String;<br><br>&nbsp;&nbsp;Money,&nbsp;Times,&nbsp;Ver,&nbsp;GroupStation,&nbsp;GroupId,&nbsp;block:&nbsp;Integer;<br>&nbsp;&nbsp;day_consumed,&nbsp;day_times,&nbsp;c_month,&nbsp;c_day,&nbsp;c_flag:&nbsp;Integer;//ER-690卡扩展参数<br>&nbsp;&nbsp;CardNo,&nbsp;CardName,CardPwd:&nbsp;Pchar;<br>&nbsp;&nbsp;sCardNo,sCardName,sCardPwd:String;<br>begin<br>&nbsp;&nbsp;.......//其它代码未贴出<br><br>&nbsp;&nbsp;&nbsp;Dllfarproc:=GetProcAddress(DllHandle,'ICCardReadEx');&nbsp;//获取函数入口<br>&nbsp;&nbsp;&nbsp;if&nbsp;DllfarProc&lt;&gt;Nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;ICCardReadExDll(Dllfarproc)(RstCon,CardNo,CardName,CardPwd,Money,Times,Day_Consumed,Day_Times,C_Month,C_Day,C_Flag,GroupStation,GroupId,Ver)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sCardNo:=StrPas(CardNo);&nbsp;//奇怪的问题在这里,未执行该行前,CardNo='00111',一执行完后,sCardNo及CardNo的值为'0011',这是为什么?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sCardName:=StrPas(CardName);&nbsp;//奇怪<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sCardPwd:=StrPas(CardPwd);
 
没人理我?大富翁的高手都到哪去了?
 
你的DLL返回CardNo,&nbsp;CardName,&nbsp;pwd时都指向了局部变量,被破坏了。<br><br>正确的做法是在&nbsp;主程序里申请足够的空间,然后DLL负责把数据复制到<br>主程序传给DLL的CardNo,&nbsp;CardName,&nbsp;pwd指向的空间。DLL声明改成下面<br>这样。<br><br>&nbsp;function&nbsp;&nbsp;ICCardReadEx(PortHandle:&nbsp;THandle;CardNo,&nbsp;CardName,&nbsp;pwd:&nbsp;PChar;var&nbsp;Money,&nbsp;Times,day_consumed,&nbsp;day_times,&nbsp;c_month,&nbsp;c_day,&nbsp;c_flag,&nbsp;GroupStation,&nbsp;GroupId,&nbsp;CardStyle:&nbsp;Integer):&nbsp;Boolean;&nbsp;stdcall;
 
后退
顶部