为什么读取INI字符串总提示出错!(100分)

M

mzwl

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么读取INI字符串总提示出错!(源码如下)<br>'C:/新建文件夹/S6.ini', 这个地址怎样才能使用相对地址?<br>错误提示“access violation at address 77e71436 in module 'kernel32.dll',<br>write of address 0043f760”<br><br>var<br>Name : pchar;<br>begin<br>Name:='';<br>GetPrivateProfileString(<br>&nbsp; &nbsp; &nbsp; &nbsp; 'PersonalInfo',<br>&nbsp; &nbsp; &nbsp; &nbsp; 'Name',<br>&nbsp; &nbsp; &nbsp; &nbsp; '',<br>&nbsp; &nbsp; &nbsp; &nbsp; Name,<br>&nbsp; &nbsp; &nbsp; &nbsp; 100,<br>&nbsp; &nbsp; &nbsp; &nbsp; 'C:/新建文件夹/S6.ini'<br>&nbsp; &nbsp; &nbsp; &nbsp; ) ;<br>Edit1.Text:=Name; <br>end;
 
PChar('C:/新建文件夹/S6.ini') 试试<br>还有使用相对路径
 
WIN98下调试没有问题啊!<br><br>估计不是这边出错吧!<br><br>C:/新建文件夹/S6.ini的内容:<br>[PersonalInfo]<br>Name=3<br><br>显示Edit1.Text等3<br><br>
 
你的Name变量是一个PChar类型,它是一个指针,还没有分配内存就使用,当然要出错了。<br>应该用GetMem()给它分配内存,FreeMem()释放,或是采用静态字符数组:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var Buf: array [0..2047] of Char;<br>begin<br>&nbsp; &nbsp; Buf := '';<br>&nbsp; &nbsp; GetPrivateProfileString('MQIS','Driver32',Buf, Buf, SizeOf(Buf),<br>&nbsp; &nbsp; &nbsp; &nbsp; 'ODBC.ini');<br>&nbsp; &nbsp; ShowMessage(Buf);<br>end;<br>
 
接受答案了.
 
顶部