OLE Storage Demo

//-------------------------------------------------------------
// STORAGE. Shows how to open structured storage document,
//          enumerate it's objects and determine if there
//          is property set objects in it.
//
//
// When     Who         What
// ----     ---         -------------------------
// 24-08-96 AF          Main program, enumeration
// 26-08-96 AF          Description from CLSID
//-------------------------------------------------------------
unit STG_Unit;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, OLE2i, StdCtrls, Registry;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    function ObjectFromCLSID(sCLSID : String) : String;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1      : TForm1;
  STG        : IStorage;
  SName      : PWideChar;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
 DocName    : Array[0..255] of WideChar;
 Enum       : IEnumStatStg;
 StatStg    : TStatStg;
 Element    : String;
 SubStream  : IStream;
 W          : POleStr;
 sCLSID     : String;
begin
  // convert to WideChar
  StringToWideChar('c:\test.doc', DocName, 256);
  // try to open storage
  If StgOpenStorage(DocName, Nil, STGM_READ OR STGM_SHARE_DENY_WRITE,
        Nil, 0, STG) <> S_OK Then
        Begin
         ShowMessage('Can''t open storage'); //Error
         Exit;
        End
  Else
   Begin
    STG.EnumElements(0, Nil, 0, Enum);           //Get enumerator
     If Enum = Nil Then ShowMessage('NO TOC')    //Error
      Else
       Begin
         While True                              //While there is elements
          Do
           Begin
           //Get next element
            If Enum.Next(1, StatStg, Nil) <> S_OK Then Exit;
            Element := WideCharToString(StatStg.pwcsName);
            If Element [1] < Chr(32) Then Element[1] := ' ';
            Case StatStg.dwType of
             1 : Element := Element + ' (Storage)';
             2 : Element := Element + ' (Stream)';
             3 : Element := Element + ' (Lockbytes)';
             4 : Element := Element + ' (Property)';
            end;
            Memo1.Lines.Add(Element);
          // clsid allows us to determine object type
            StringFromCLSID(StatStg.clsid, W);
            If (StatStg.dwType) In [2..3] Then //streams does not have CLSID!!
            Else
             Begin
              sCLSID := WideCharToString(W);
              Memo1.Lines.Add(#9+sCLSID);
              Memo1.Lines.Add(#9+ObjectFromCLSID(sCLSID));
             End;
           End;
       End;
   End;
end;
//
// Try to open registry entry to get description
// from CLSID
function TForm1.ObjectFromCLSID;
Var
 Reg : TRegistry;
Begin
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_CLASSES_ROOT;
 Reg.OpenKey('\CLSID\'+sCLSID, False);
 Result := Reg.ReadString('');
 Reg.CloseKey;
 Reg.Free;
End;
end.