...convert variant to Stream and then back?

Author: ari
Homepage: http://borland.mblogger.cn/aiirii

Category: Misc

// 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));
 //is it the best way to get the Variant's length?
  
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;

  //--------------------------------------------------------------------
  //notice: I have asked this question on BDN, and David Lewis told me
  //        to use the function VarArrayLock.

 

printed from
www.swissdelphicenter.ch
developers knowledge base