Delphi内存拷贝与记录到另一个记录

sMa*_*Mah 4 memory delphi copy record

我遇到逻辑问题.我不知道如何将记录复制到Delphi中的另一条记录.

TypeA = record
  value1 : word;
  value2 : word;
  value3 : word;
  end;

TypeB = record
  b1 : byte;
  b2 : byte;    
  end;
Run Code Online (Sandbox Code Playgroud)

我有两个记录TypeA和TypeB.例如,我发现TypeA记录中的数据属于TypeB记录.注意:TypeA具有更长的数据长度.

问题:如何复制TypeA内存并将其放在TypeB记录中?

CopyMemory(@TypeA, @TypeB, Length(TypeB))
Run Code Online (Sandbox Code Playgroud)

当我尝试CopyMemory并得到一个错误(无法比较的类型).

PS:我不想像下面那样复制或分配它.

TypeB.b1:= TypeA.value1 && $ FF;

TypeA和TypeB只是示例记录.大多数情况下,记录TypeA和TypeB可能包含多个记录,并且分配表单TypeA并分配给TypeB记录将更加困难.

提前致谢

----加法问题:

有没有办法将Delphi记录复制到Byte数组以及如何?如果有,

  • TypeA记录到字节数组
  • 字节数组到B类

这个逻辑会起作用吗?

And*_*and 7

CopyMemory(@a, @b, SizeOf(TypeB))
Run Code Online (Sandbox Code Playgroud)

if a是类型TypeAb类型TypeB.

  • 它还需要一个警告:如果`TypeB`包含托管类型的任何字段,这会搞乱Delphi的使用计数器,并可能导致无法追踪的`AV`. (12认同)
  • 我赞成这一点,但有一些解释会更好.否则它看起来就像魔术一样. (3认同)

Mar*_*mes 5

变体记录:

TypeA = packed record
  value1 : word;
  value2 : word;
  value3 : word;
  end;

TypeB = packed record
  b1 : byte;
  b2 : byte;
  end;

TypeAB = packed record
  case boolean of
    false:(a:TypeA);
    true:(b:TypeB);
end;
..
..
var someRec:TypeAB;
    anotherRec:TypeAB;
..
..
  anotherRec.b:=someRec.b
Run Code Online (Sandbox Code Playgroud)