如何将对象复制到指定的内存地址处
我写了下面这段,试图复制一TButton对象到指定的内存地址处,但该函数执行完总是产生异常,各位大侠帮忙看一看, 有没有良策实现:
procedure TForm1.Button2Click(Sender: TObject);
var
B1, B2, B3: TButton;
I1, I2: ^TButton; //TButton *TButton
B: ^TButton;
IntfTable: PInterfaceTable;
ClassPtr: TClass;
I: Integer;
A: Array[0..439] of Integer;
begin
B1 := TButton.Create(nil);
B1.Parent := nil;
B1.Caption := 'B1';
B2 := TButton.Create(nil);
B2.Left := 100;
B2.Parent := Self;
B2.Caption := 'B2';
B3 := B1;
B3.Free;
//第一种方式
for I := 0 to TButton.InstanceSize -1 do
PInteger(@PChar(B1)[I])^ := PInteger(@PChar(B2)[I])^;
//第二种方式
{
FillChar(B^, TButton.InstanceSize, 0);
PInteger(B)^ := Integer(TButton);
ClassPtr := TButton;
while ClassPtr <> nil do
begin
IntfTable := ClassPtr.GetInterfaceTable;
if IntfTable <> nil then
for I := 0 to IntfTable.EntryCount - 1 do
with IntfTable.Entries[I] do
begin
if VTable <> nil then
PInteger(@PChar(B)[IOffset])^ := Integer(VTable);
end;
ClassPtr := ClassPtr.ClassParent;
end;
B.Parent := B2.Parent;
B.Caption := B2.Caption;
}
end;