我如何为来自Stream的Olevariant变量赋值?

时间:2010-11-19 14:32:32

标签: delphi memory-management stream variant

我需要从Stream读取并将缓冲区重新加入OleVariant(VarArray)变量。

var
MemoryStream : TMemoryStream;
Data : OleVariant;
begin
            MemoryStream:=TMemoryStream.Create;
            try
                FuncFill(MemoryStream); //Filling the stream
                MemoryStream.Seek(0,0);
                MemoryStream.Read(Data, MemoryStream.Size);//this line lock tha app, i need allocate the memory for the OleVariant variable?
            finally
             MemoryStream.Free;
            end;

end;

问题是我如何将TMemoryStream中的readed值分配给olevariant变量?

我正在使用delphi 5.

1 个答案:

答案 0 :(得分:6)

您可以使用VarArrayLock函数获取指向OleVariant数据的指针,然后读取此指针。

使用VarArrayvarByte元素检查此代码。

var
 MemoryStream : TMemoryStream;
 Data : OleVariant;
 DataPtr : Pointer;
begin
   MemoryStream:=TMemoryStream.Create;
     try
      FuncFill(MemoryStream); //Filling the stream
      MemoryStream.Seek(0,0);
          Data    :=VarArrayCreate([0, MemoryStream.Size - 1], varByte);
      DataPtr     :=VarArrayLock(Data);
       try
         MemoryStream.ReadBuffer(DataPtr^,MemoryStream.Size); //Get the pointer to the variant variable.
       finally
         VarArrayUnlock(Data); //when you are done , you must call to VarArrayUnlock
       end;
    finally
      MemoryStream.Free;
    end;    
end;