如何将对象复制到指定的内存地址处

cscheng7907 2005-09-19 08:59:10
我写了下面这段,试图复制一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;
...全文
151 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lichaohui 2005-09-23
  • 打赏
  • 举报
回复
直接复制是会有问题的,你绕开了Delphi的一些机制,
会破坏某些变量在内存中的状态,譬如,字符串

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧