随便找了一段,Record和Component是一个意思
function ComponentToVariant(AComponent: TComponent): OleVariant;
var
BinStream: TMemoryStream;
Data: Pointer;
begin
BinStream := TMemoryStream.Create;
try
BinStream.WriteComponent(AComponent);
result := VarArrayCreate([0, BinStream.Size-1], varByte);
Data := VarArrayLock(result);
try
Move(BinStream.Memory^, Data^, BinStream.Size);
finally
VarArrayUnlock(result);
end;
finally
BinStream.Free;
end;
end;
function VariantToComponent(AValue: OleVariant): TComponent;
var
BinStream: TMemoryStream;
Data: Pointer;
begin
BinStream := TMemoryStream.Create;
try
Data := VarArrayLock(AValue);
try
BinStream.WriteBuffer(Data^, VarArrayHighBound(AValue, 1)
+1);
finally
VarArrayUnlock(AValue);
end;
BinStream.Seek(0, soFrombegin
ning);
result := BinStream.ReadComponent(nil);
finally
BinStream.Free;
end;
end;