为什么现在提问都没有来帮忙?为什么?分太少吗?????? <各位来帮帮忙啊,自定义结构中,PCHAR类型如何保存值?(30分)

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

taizhi

Unregistered / Unconfirmed
GUEST, unregistred user!
//有下面一自定义结构<br>type<br>&nbsp; PDevInfo=^TDevInfo;<br>&nbsp; TDevInfo=packed record<br>&nbsp; &nbsp; PortHandle &nbsp; &nbsp;: &nbsp;Integer;<br>&nbsp; &nbsp; CardPwdType &nbsp; : &nbsp;Integer; //卡密码类型<br>&nbsp; &nbsp; DevicePwd &nbsp; &nbsp; : &nbsp;Pchar; //设备密码<br>&nbsp; &nbsp; CardPwd &nbsp; &nbsp; &nbsp; : &nbsp;Pchar; //卡密码<br>&nbsp; end;<br>//有一对自定义结构赋值的函数<br>function TfrmDevMain.InitDevInfo(MyDevInfo:TDevInfo):Boolean;<br>var<br>&nbsp; CommuPort:String;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; if Aq_DevDetail.RecordCount&gt;0 then<br>&nbsp; With Aq_DevDetail do<br>&nbsp; begin &nbsp; &nbsp;<br>&nbsp; &nbsp; MyDevInfo.PortHandle :=-1;<br>&nbsp; &nbsp; MyDevInfo.CardPwdType :=FieldByName('CardPwdType').AsInteger;<br>&nbsp; &nbsp; if MyDevInfo.CardPwdType =0 then<br>&nbsp; &nbsp; &nbsp; MyDevInfo.CardPwd :=Pchar(FieldByName('APwd').AsString)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; MyDevInfo.CardPwd :=Pchar(FieldByName('BPwd').AsString);<br>&nbsp; &nbsp; MyDevInfo.DevicePwd :=Pchar(FieldByName('DevicePwd').AsString);<br>&nbsp; &nbsp; Result:=True;<br>&nbsp; end;<br>end;<br><br>//下面是调用函数代码<br>procedure TfrmDevMain.dxBarBtnRefreshClick(Sender: TObject);<br>var<br>&nbsp; MyDev:TDevInfo;<br>begin<br>&nbsp; InitDevInfo(MyDev);<br>&nbsp; Edit1.Text:=MyDev.CardPwd //此时的Edit1.Text并不是Aq_DevDetail对应字段的值,是乱码为什么?<br>end;<br><br>请各位高手帮帮忙,是否在调用 InitDevInfo之前,必须对MyDev分配内存?如果是,又该如何分配呢?
 
PCHAR 是 Char类型的指针的意思,<br>指针是一个内存地址,<br>看你的代码,你存放在指针里面的是一个dataset的单元内容地址,<br>如果这个dataset所在内存没有被free,是有效的<br>取的话应该 Edit1.text := MyDev.CardPwd^; //^ 表示取内容<br><br>任何数据都是存储在内容的 <br>@就是得到数据存放的内存位置 <br>@相当于Addr()函数 <br>而^就是指该内存位置存储的内容
 
你应该对结构体分配内存啊,
 
把 PChar 改成 string 即可。
 
type<br>&nbsp; PDevInfo=^TDevInfo;<br>&nbsp; TDevInfo=packed record<br>&nbsp; &nbsp; PortHandle &nbsp; &nbsp;: &nbsp;Integer;<br>&nbsp; &nbsp; CardPwdType &nbsp; : &nbsp;Integer; //卡密码类型<br>&nbsp; &nbsp; DevicePwd &nbsp; &nbsp; : &nbsp;string; //设备密码<br>&nbsp; &nbsp; CardPwd &nbsp; &nbsp; &nbsp; : &nbsp;string; //卡密码<br>&nbsp; end;<br>//有一对自定义结构赋值的函数<br>function TfrmDevMain.InitDevInfo([red]var[/red] MyDevInfo:TDevInfo):Boolean;<br>var<br>&nbsp; CommuPort:String;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; if Aq_DevDetail.RecordCount&gt;0 then<br>&nbsp; With Aq_DevDetail do<br>&nbsp; begin &nbsp; &nbsp;<br>&nbsp; &nbsp; MyDevInfo.PortHandle :=-1;<br>&nbsp; &nbsp; MyDevInfo.CardPwdType :=FieldByName('CardPwdType').AsInteger;<br>&nbsp; &nbsp; if MyDevInfo.CardPwdType =0 then<br>&nbsp; &nbsp; &nbsp; MyDevInfo.CardPwd :=FieldByName('APwd').AsString<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; MyDevInfo.CardPwd :=FieldByName('BPwd').AsString;<br>&nbsp; &nbsp; MyDevInfo.DevicePwd :=FieldByName('DevicePwd').AsString;<br>&nbsp; &nbsp; Result:=True;<br>&nbsp; end;<br>end;
 
如果是调用VC写的DLL,就必须使用PCHAR,看你的代码是在初试化硬件的东西,你就必须申明为PCHAR,这才是对的,转到DELPHI中,可以使用STRING,进行类型强制转化就可以了!或者修改string中的结尾标志 /0
 
string类型是错误的说法,不同语言的dll调用还是pchar,当然知道大小的话,也可以使用array,比如<br>type<br>&nbsp; PDevInfo=^TDevInfo;<br>&nbsp; TDevInfo=packed record<br>&nbsp; &nbsp; PortHandle &nbsp; &nbsp;: &nbsp;Integer;<br>&nbsp; &nbsp; CardPwdType &nbsp; : &nbsp;Integer; //卡密码类型<br>&nbsp; &nbsp; DevicePwd &nbsp; &nbsp; : &nbsp;array[0..7] of char; //设备密码 ,8位<br>&nbsp; &nbsp; CardPwd &nbsp; &nbsp; &nbsp; : &nbsp;array[0..7] of char; //卡密码,8位<br>&nbsp; end;
 
Edit1.Text:= StrPas(MyDev.CardPwd);
 
后退
顶部