Delphi中Variant和Stream的互换
2009-2-8 9:07:36 来源: 转载 作者:wong aiirii 访问:333 次 被顶:3 次 字号:【大 中 小】
核心提示:在Com, Dcom, Com+中经常要用到variant, 而在delphi中的文件操作之类中, 更多的用到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;
本文来自Delphi之窗,原文地址:
http://www.52delphi.com