//TARec--->Variant
function RecordToVariant(value:TParamRecord):OleVariant;
var P:Pointer;
begin
Result:=VarArrayCreate([0,sizeof(TParamRecord)],varByte);
P:=VarArrayLock(Result);
Move(value,P^,SizeOf(TParamRecord));
VarArrayUnlock(Result);
end;
//OleVariant---->TARec
function VariantToRecord(value:OleVariant):TParamRecord;
var P:Pointer;
begin
P:=VarArrayLock(value);
Move(P^,Result,SizeOf(TParamRecord));
VarArrayUnlock(value);
end;
好像不好使。先留着。
以下不知道好使否,也先留着。
http://dev.csdn.net/develop/article/53/53251.shtm
Variant --> Stream
var ov: OleVariant;
ms: TStream; p: Pointer;
begin
ov :=dmMain.ComConnection.AppServer.TimeZone;
ms := TMemoryStream.Create;
ms.Position := 0;
p := VarArrayLock(ov);
ms.Write(p^, VarArrayHighBound(ov, 1));//這句可否改進?
VarArrayUnlock(ov);
ms.Position := 0;
...
ms.Free;
end;
Stream --> Variant
function TTCanteenSvr.Get_TimeZone: OleVariant;
var
AStream: TStream;
MyBuffer: Pointer;
begin
try
AStream :=TFileStream.Create(, fmOpenRead);
Result :=VarArrayCreate([0, AStream.Size - 1], VarByte);
MyBuffer :=VarArrayLock(Result);
AStream.ReadBuffer(MyBuffer^, AStream.Size);
VarArrayUnlock(Result);
finally
AStream.Free;
end;
Record --> Stream
var r,r2:TStockInfo;p:Pointer;iRecSize,StringSize:Integer;
pc:PChar;
s:string;v:Variant;
ms:TMemoryStream;
begin
// TStockInfo=record
//SecurityID:string[12];
//CompanyID:string[12];
//InternalID:string[12];
// end;
// 赋值Record
r.SecurityID:='123';
r.CompanyID:='456';
r.InternalID:='789';
iRecSize:=SizeOf(TStockInfo);
StringSize:=SizeOf(string);
GetMem(p,iRecSize);
GetMem(pc,StringSize);
ms:=TMemoryStream.Create;
// Move测试
Move(r,p^,iRecSize); //赋值到指针
Move(p^,r2,iRecSize); // 赋值到验证Record
ms.WriteBuffer(p,iRecSize);
ms.SaveToFile(AppPath+'test.txt');
//ShowMessage(r2.SecurityID+','+r2.CompanyID+','+r2.InternalID);
// 移动指针
// 这里需要转换指针类型,string->PCharInteger->PInteger Double->PDouble等等
//Inc(PChar(p),StringSize); //跳过SecurityID
// 赋值CompanyID
// Move(P^,pc,StringSize);
// Move(P^,s,StringSize);
// ShowMessage(StrPas(pc));
// Dec(PChar(p),StringSize); // 回来
FreeMem(p);// 并且释放
ms.Free;
end;