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


Sub-Storages

Now that you know how to read and write data to/from a stream it is time to move on to storages. Once again think of a doc file as a file system, the storages are then like directories. Storages make it possible to store data in a structured hierarchy.

To demonstrate using storages here is a simple application that saves the text from 3 memos to a doc file.


Control Caption Name
TPageControl   PageControl1
TMemo   mem_Page1
TMemo   mem_Page2
TMemo   mem_Page3

Each of the memos is on a tab of the TPageControl. The data is loaded in the forms's OnShow event and saved in the OnHide event.

Each memo's data is stored in a stream called Data each in a separate storage separate storage. The hierarchy looks like this


   procedure TForm1.FormShow(Sender: TObject);

      procedure LoadPage(  Parent : IStorage;  sName : WideString;  Memo : TMemo  );
      var
         Hr : HResult;
         Stor : IStorage;
         Stream : IStream;
         OleStream : TOleStream;
      begin
            {Open the storage}
         Hr := Parent.OpenStorage(  PWideChar(sName),
                                    nil,
                                    STGM_READWRITE or
                                    STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                                    nil,
                                    0,
                                    Stor
                                  );

            {Was is opened?}
         if(   not SUCCEEDED(  Hr  )   ) then
         begin
            (*  Fail  *)
            Exit;
         end;

            {Try to open the stream}
         Hr := Stor.OpenStream(  'Data',
                                  nil,
                                  STGM_READWRITE or
                                  STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                                  0,
                                  Stream
                                );

            {Was is opened?}
         if(   not SUCCEEDED(  Hr  )   ) then
         begin
            (*  Fail  *)
            Exit;
         end;

            {Create the OleStream}
         OleStream := TOleStream.Create(  Stream  );

            {Load the data}
         Memo.Lines.LoadFromStream(  OleStream  );

            {Finished with the OleStream stream}
         OleStream.Free;
      end;

   var   
      Hr : HResult;
      RootStorage : IStorage;
   begin
         {Try open the DocFile}
      Hr := StgOpenStorage(  'c:\Temp\MyDocFile.ole',
                             nil,
                             STGM_READWRITE or
                             STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                             nil,
                             0,
                             RootStorage
                           );

         {Was is opened?}
      if(   not SUCCEEDED(  Hr  )   ) then
      begin
         (*  Fail  *)
         Exit;
      end;

         {Load the 3 pages}
      LoadPage(  RootStorage,  '1',  mem_Page1  );
      LoadPage(  RootStorage,  '2',  mem_Page2  );
      LoadPage(  RootStorage,  '3',  mem_Page3  );
   end;

The FormShow procedure above opens the doc file and then loads the data for the 3 memos by calling the LoadPage nested function. The LoadPage function is simple enough and by now you should understand what it does. The only thing that needs discussing is the OpenStorage method.


   Hr := Parent.OpenStorage(  1 -   wsName,
                              2 -   nil,
                              3 -   STGM_READWRITE or
                                    STGM_DIRECT or STGM_SHARE_EXCLUSIVE,
                              4 -   nil,
                              5 -   0,
                              6 -   Stor
                            );

1 Name of the sub-storage
2 "it is a pointer to a previous opening of an element of the storage object"
This will normally just nil
3 Access mode
4 "points to a block of stream names in this storage object that are to be emptied as the object is opened"
Again this is normally nil
5 Reserved must be 0
6 The storage to be opened






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