Author: Tomas Rutkauskas How to convert a TMemoryStream to an OLE variant and vice versa Answer: 1 2 function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant; 3 var 4 Data: PByteArray; 5 begin 6 Result := VarArrayCreate([0, Strm.Size - 1], varByte); 7 Data := VarArrayLock(Result); 8 try 9 Strm.Position := 0; 10 Strm.ReadBuffer(Data^, Strm.Size); 11 finally 12 VarArrayUnlock(Result); 13 end; 14 end; 15 16 function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream; 17 var 18 Data: PByteArray; 19 Size: integer; 20 begin 21 Result := TMemoryStream.Create; 22 try 23 Size := VarArrayHighBound(OV, 1) - VarArrayLowBound 24 (OV, 1) + 1; 25 Data := VarArrayLock(OV); 26 try 27 Result.Position := 0; 28 Result.WriteBuffer(Data^, Size); 29 finally 30 VarArrayUnlock(OV); 31 end; 32 except 33 Result.Free; 34 Result := nil; 35 end; 36 end;