program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;
type
TMan = record
Name: string;
Age: byte;
end;
procedure RecordToVariant(var ARec; Size: integer; var v: Variant);
var
p: PByteArray;
prec: Pointer;
i: integer;
begin
v := VarArrayCreate([0, Size -1], varByte);
p := VarArrayLock(v);
prec := @ARec;
for i:=0 to Size-1 do
p^ := (PByteArray(prec))^;
VarArrayUnLock(v);
end;
procedure VariantToRecord(v: Variant; var ARec; Size: integer);
var
i: integer;
p: PByteArray;
prec: Pointer;
begin
prec:=@ARec;
p := VarArrayLock(v);
for i:=0 to Size-1 do
(PByteArray(prec))^ := p^;
VarArrayUnLock(v);
end;
var
a, b: TMan;
v: Variant;
begin
a.name := 'Tom';
a.Age := 10;
RecordToVariant(a, SizeOf(a), v);
//把v发送到网络等等操作
//......
VariantToRecord(v, b, SizeOf(b));
writeln(b.name);
readln;
end.