This site is moving to The Zen Source Library
Please Update your bookmarks


Reading data from a stream (continued)

The storage is open so we can read the data, or can we… ? Here is the function used to read data from a stream


   IStream.Read(  Buffer for data,
                  Number of bytes to read,
                  Number of bytes that were actually read
               ); 

What we need to know first is the number of bytes to read. Since all the stream contains is the memo's text the amount of data to read is the same as the size of the stream.


Here is a function to return the size of a open stream


   function GetStreamSize(  Stream : IStream  ) : LongInt;
   var
      Hr : HResult;
      StatStg : TStatStg;
   begin

         {Get the STATSTG info for the stream.
           Dont return the name (saves mem alloc & dealloc)}
      Hr := Stream.Stat(  StatStg,  STATFLAG_NONAME  );


         {Success?}
      if(   not SUCCEEDED(  Hr  )   ) then
      begin
         Result := -1;
         Exit;
      end;


         {Get the size as a LongInt}
      Result := Round(  StatStg.cbSize  );

   end;


Calling IStream.Stat returns quite a bit of info on the stream, this included the stream size. The stream's name is also returned unless the STATFLAG_NONAME is specified. The reason for using this flag, if you dont want the name returned, is that it saves unnecessary memory allocations and deallocations used for returning the name.

Reading the data is now possible since we have all the necessary information. As with writing data to a stream, using a TOleStream to read the data will make everything much easier.

The full source code for this example is available as a zip file on the source code page






All information on these www pages is copyright (©) 1997 Andre .v.d. Merwe And may not be copied or mirrored without my permission.