L
lwen
Unregistered / Unconfirmed
GUEST, unregistred user!
请各位大侠解决在TMemoryStream.Read时,Buffer地址发生变化,退出事件时报错。
代码如下
procedure TForm1.Button1Click(Sender: TObject);
type
PMyRec = ^TMyRec;
TMyRec = Record
a: Integer;
b: Double;
c: array[0..7] of Char;
end;
var
MStream: TMemoryStream;
pBuffer, lBuffer: PMyRec;
aRecord, rRecord: TMyRec;
begin
pBuffer := New(PMyRec);
pBuffer := @aRecord;
lBuffer := New(PMyRec);
lBuffer := @rRecord;
with aRecord do
begin
a := 12345;
b := pi;
c := 'Hello'#0;
end;
with rRecord do
begin
a := 54321;
b := pi+111;
c := 'dasd'#0;
end;
ShowMessage(format('a: %d, b: %f, c: %s', [aRecord.a, aRecord.b, aRecord.c]));
ShowMessage(format('a: %d, b: %f, c: %s', [pBuffer.a, pBuffer.b, pBuffer.c]));
MStream := TMemoryStream.Create;
MStream.Position := 0;
MStream.Write(pBuffer, SizeOf(TMyRec));
MStream.Position := 0;
MStream.Read(lBuffer, SizeOf(TMyRec));
CopyMemory(@rRecord, lBuffer, SizeOf(TMyRec));
ShowMessage(format('a: %d, b: %f, c: %s', [rRecord.a, rRecord.b, rRecord.c]));
ShowMessage(format('a: %d, b: %f, c: %s', [lBuffer.a, lBuffer.b, lBuffer.c]));
MStream.Free;
end;
出错信息:raised exception class EPrivilege with message 'Privileged instruction'.
代码如下
procedure TForm1.Button1Click(Sender: TObject);
type
PMyRec = ^TMyRec;
TMyRec = Record
a: Integer;
b: Double;
c: array[0..7] of Char;
end;
var
MStream: TMemoryStream;
pBuffer, lBuffer: PMyRec;
aRecord, rRecord: TMyRec;
begin
pBuffer := New(PMyRec);
pBuffer := @aRecord;
lBuffer := New(PMyRec);
lBuffer := @rRecord;
with aRecord do
begin
a := 12345;
b := pi;
c := 'Hello'#0;
end;
with rRecord do
begin
a := 54321;
b := pi+111;
c := 'dasd'#0;
end;
ShowMessage(format('a: %d, b: %f, c: %s', [aRecord.a, aRecord.b, aRecord.c]));
ShowMessage(format('a: %d, b: %f, c: %s', [pBuffer.a, pBuffer.b, pBuffer.c]));
MStream := TMemoryStream.Create;
MStream.Position := 0;
MStream.Write(pBuffer, SizeOf(TMyRec));
MStream.Position := 0;
MStream.Read(lBuffer, SizeOf(TMyRec));
CopyMemory(@rRecord, lBuffer, SizeOf(TMyRec));
ShowMessage(format('a: %d, b: %f, c: %s', [rRecord.a, rRecord.b, rRecord.c]));
ShowMessage(format('a: %d, b: %f, c: %s', [lBuffer.a, lBuffer.b, lBuffer.c]));
MStream.Free;
end;
出错信息:raised exception class EPrivilege with message 'Privileged instruction'.