在Com, Dcom, Com+中經常要用到variant, 而在delphi 中的文件操作之類中, 更多的用到Stream, 有時, 需要適當的互換!
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;