J
Jebit
Unregistered / Unconfirmed
GUEST, unregistred user!
这一段的代码是会出错的。
procedure TForm1.Button1Click(Sender: TObject);
var existedFile:string;
buffer:array of char;
reg:TRegistry;
newfile:TFileStream;
begin
if edit1.Text[length(edit1.text)]='/'
then
existedFile:=edit1.text+'New Oriental Words.cfg'
else
existedFile:=edit1.text+'/New Oriental Words.cfg';
newfile:=TFileStream.Create(existedFile,fmOpenRead);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
try
if reg.OpenKey('Software/J-Studio',true)
then
begin
setlength(buffer,newfile.size);
newfile.ReadBuffer(buffer,newfile.size);
reg.WriteBinaryData('test',buffer,newfile.size);
end;
finally
reg.free;
newfile.free;
end;
end;
当我把上边的代码改成下边的,就不会出错了。
procedure TForm1.Button1Click(Sender: TObject);
var existedFile:string;
buffer:array[00..256] of char;
reg:TRegistry;
newfile:TFileStream;
begin
if edit1.Text[length(edit1.text)]='/'
then
existedFile:=edit1.text+'New Oriental Words.cfg'
else
existedFile:=edit1.text+'/New Oriental Words.cfg';
newfile:=TFileStream.Create(existedFile,fmOpenRead);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
try
if reg.OpenKey('Software/J-Studio',true)
then
begin
newfile.ReadBuffer(buffer,newfile.size);
reg.WriteBinaryData('test',buffer,newfile.size);
end;
finally
reg.free;
newfile.free;
end;
end;
我不知道为什么用动态数组会有错。
后来我又写了一段类似的代码。更奇怪了。如下
procedure TForm1.Button1Click(Sender: TObject);
begin
self.writefile2reg('d:/1.txt','software/J-Studio','test');
end;
procedure TForm1.writefile2reg(filename:string;keyname:string;value:string);
var tempfile:TFileStream;
bufferchar;
reg:TRegistry;
begin
tempfile:=TFileStream.create(filename,fmOpenRead);
tempfile.Seek(0,soFromBeginning );
getmem(buffer,tempfile.size);
tempfile.ReadBuffer(buffer,tempfile.size);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
reg.OpenKey(keyname,true);
reg.WriteBinaryData(value,buffer,tempfile.size);
reg.Free;
// freemem(buffer)
//当加上freemem的时候会出错,当不加的时候就不会出错了。
tempfile.free;
end;
在这段程序里边,把buffer改成动态数组还是会出错,用静态的依然不会出错。
太奇怪了。
procedure TForm1.Button1Click(Sender: TObject);
var existedFile:string;
buffer:array of char;
reg:TRegistry;
newfile:TFileStream;
begin
if edit1.Text[length(edit1.text)]='/'
then
existedFile:=edit1.text+'New Oriental Words.cfg'
else
existedFile:=edit1.text+'/New Oriental Words.cfg';
newfile:=TFileStream.Create(existedFile,fmOpenRead);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
try
if reg.OpenKey('Software/J-Studio',true)
then
begin
setlength(buffer,newfile.size);
newfile.ReadBuffer(buffer,newfile.size);
reg.WriteBinaryData('test',buffer,newfile.size);
end;
finally
reg.free;
newfile.free;
end;
end;
当我把上边的代码改成下边的,就不会出错了。
procedure TForm1.Button1Click(Sender: TObject);
var existedFile:string;
buffer:array[00..256] of char;
reg:TRegistry;
newfile:TFileStream;
begin
if edit1.Text[length(edit1.text)]='/'
then
existedFile:=edit1.text+'New Oriental Words.cfg'
else
existedFile:=edit1.text+'/New Oriental Words.cfg';
newfile:=TFileStream.Create(existedFile,fmOpenRead);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
try
if reg.OpenKey('Software/J-Studio',true)
then
begin
newfile.ReadBuffer(buffer,newfile.size);
reg.WriteBinaryData('test',buffer,newfile.size);
end;
finally
reg.free;
newfile.free;
end;
end;
我不知道为什么用动态数组会有错。
后来我又写了一段类似的代码。更奇怪了。如下
procedure TForm1.Button1Click(Sender: TObject);
begin
self.writefile2reg('d:/1.txt','software/J-Studio','test');
end;
procedure TForm1.writefile2reg(filename:string;keyname:string;value:string);
var tempfile:TFileStream;
bufferchar;
reg:TRegistry;
begin
tempfile:=TFileStream.create(filename,fmOpenRead);
tempfile.Seek(0,soFromBeginning );
getmem(buffer,tempfile.size);
tempfile.ReadBuffer(buffer,tempfile.size);
reg:=TRegistry.Create;
reg.RootKey:=HKEY_CURRENT_USER;
reg.OpenKey(keyname,true);
reg.WriteBinaryData(value,buffer,tempfile.size);
reg.Free;
// freemem(buffer)
//当加上freemem的时候会出错,当不加的时候就不会出错了。
tempfile.free;
end;
在这段程序里边,把buffer改成动态数组还是会出错,用静态的依然不会出错。
太奇怪了。