C++ Source Code

on Michael Vorburger's Personal Homepage
Home Personal Projects alpha ware

 

This is a part of the SumInf source code in C++. Please note that SumInf is © Copyright. This code is not enough to compile the DLL, important header files and helper functions have been left out.

You are not allowed to use it for your own purpose. However, if you plan on implmenting something similar to SumInfo, you might want to contact alpha ware. (More Info about SumInf)

/*******************************************************************************
** SUMINFO - Tiny Library To Access OLE Summary Information
**
** AUTHOR: Michael Vorburger
** DATE:   11/11/96
**
** LAST MODIFIED: 30/11/96
**                   - SummaryInfo::Load : StgOpenStorage NOW USES
**                       STGM_PRIORITY instead STGM_SHARE_DENY_WRITE. This
**                       allows to work on open documents and is probably
**                       even faster. (According to OLE Reference)
**                   - SummaryInfo::Load : AFTER delete[] m_pSumInfo;
**                                         NEW m_pSumInfo = NULL;
**                       The old (released) memory was accessed by GetString
**                       when =NULL was forgotten! That's okay now.
**
** COMMENTS
**
** Tested for 16-Bit only.
** Should compile (with slight adaptations) under 32-Bit as well.
**
** The SummaryInfo class does not yet support WRITING or MODIFYING of suminfos.
**
** Microsoft provides a sample code SUMINFO which allows to read & modify all
** Summary Info. After several attempts, that sample is finally not used here
** because it does not compile under 16-Bit/BORLAND.
** Instead, the Summary Info is read & interpreted "from scratch" corresponding
** to the specification in Win32 SDK: OLE Property Sets/The Document SumInf.
**
** OLE Programming "inspired" by MS examples and Brockschmidt (Inside OLE 2)
**
** (c) Oct./Nov. 1996 by alpha ware, Michael Vorburger (Switzerland)
**     for XXX
*******************************************************************************/

#include "suminfo.h"


/**********************************************************************
**  UINT   SummaryInfo::Load(LPSTR pszFile)
**
**  Reads the property stream of a compound file into memory
**  and prepares SummaryInfo data members for subsequent access.
*/
UINT SummaryInfo::Load(LPSTR pszFile)
{
    HRESULT             hr;
    LPSTORAGE           pIStorage;
    LPSTREAM            pIStream;
    STATSTG             StreamStat;
    ULONG               ulBytesRead;

    if (NULL!=m_pSumInfo)
          { delete[] m_pSumInfo;  m_pSumInfo = NULL; }

/*
#if defined(WIN32) && !defined(UNICODE)
    OLECHAR pwcsFile[MAX_PATH];
    mbstowcs(pwcsFile, pszFile, MAX_PATH);
    hr=StgOpenStorage(pwcsFile, NULL, STGM_DIRECT | STGM_READWRITE
        | STGM_SHARE_EXCLUSIVE, NULL, 0, &pIStorage);
#else
*/
    hr=StgOpenStorage(pszFile, NULL,
                       STGM_READ | STGM_DIRECT | STGM_PRIORITY,
                      NULL, 0, &pIStorage);
// #endif

    if (FAILED(hr))
        return SUMINFO_COULDNOTOPEN;

    hr=pIStorage->OpenStream(OLESTR("\005SummaryInformation"),
                NULL,
                STGM_READ | STGM_DIRECT | STGM_SHARE_EXCLUSIVE,
                0,
                &pIStream);

    if (FAILED(hr))
        { if (NULL!=pIStorage) pIStorage->Release();
          return SUMINFO_NOSUMMARYINFO;               };

    hr=pIStream->Stat(&StreamStat, STATFLAG_NONAME);

    if (FAILED(hr))
        { if (NULL!=pIStream)  pIStream->Release();
          if (NULL!=pIStorage) pIStorage->Release();
          return SUMINFO_STATTROUBLE;                };

    m_SumInfoSize=StreamStat.cbSize.LowPart;
    m_pSumInfo = new char[m_SumInfoSize];               // allocate mem block

    hr=pIStream->Read(m_pSumInfo, m_SumInfoSize, &ulBytesRead);
    if (ulBytesRead != m_SumInfoSize)
        m_SumInfoSize=ulBytesRead;   // less bytes read than expected! (???)

    if (FAILED(hr))
        { m_pSumInfo=NULL; m_SumInfoSize=0; };

    if (NULL!=pIStream)
         pIStream->Release();

    if (NULL!=pIStorage)
         pIStorage->Release();

    // At this point, the summary stream is entirely read and closed again.
    // It is now up to CheckAndInterpret() to check the header and set some
    // class members to indicate the number of properties etc.

    return CheckAndInterpret();
};


/**********************************************************************
** UINT SummaryInfo::CheckAndInterpret()
**
** Protected member function. Called by Load; never by end-user.
*/
UINT SummaryInfo::CheckAndInterpret()
{
    if (   (((PROPERTYSETHEADER*)m_pSumInfo)->wByteOrder != 0xFFFE)
         | (((PROPERTYSETHEADER*)m_pSumInfo)->wFormat    != 0)
         | (((PROPERTYSETHEADER*)m_pSumInfo)->reserved    < 1) )
         { m_pSumInfo=NULL; m_SumInfoSize=0;
         return SUMINFO_WRONGHEADER;            };

    // No idea why +2 below; but miscalculates otherwise... (aligment???)
    FORMATIDOFFSET*  pFormatIDOffset = m_pSumInfo + sizeof(PROPERTYSETHEADER)+2;

    // Could/Should now check if pFormatIDOffset->fmtid equals
    // F29F85E0-4FF9-1068-AB91-08002B27B3D9 = DEFINE_GUID(FormatID_SummaryInformation,
    // 0xF29F85E0, 0x4FF9, 0x1068, 0xAB, 0x91, 0x08, 0x00, 0x2B, 0x27, 0xB3, 0xD9);

    m_pSection       = m_pSumInfo + pFormatIDOffset->dwOffset;
    m_Properties     = ((PROPERTYSECTIONHEADER*)m_pSection)->cProperties;
    m_pIdOffsetArray = m_pSection + sizeof(PROPERTYSECTIONHEADER);

    return SUMINFO_OK;
};


/**********************************************************************
** char* SummaryInfo::GetString(DWORD PropertyID)
**
** This function searches and returns a property of type string in
** a property set that was loaded into memory by SummaryInfo::Load.
*/
char* SummaryInfo::GetString(DWORD PropertyID)
{
   if (NULL==m_pSumInfo)
      return NULL;

   for (int i=0; i<m_Properties; i++)
   {
      if( m_pIdOffsetArray[i].propid == PropertyID )
      {
         DWORD* pdwType = (DWORD*)(m_pSection + m_pIdOffsetArray[i].dwOffset);
         char*  pValue = (m_pSection + m_pIdOffsetArray[i].dwOffset + sizeof(DWORD));
         if (*pdwType == VT_LPSTR )
           return pValue + sizeof(DWORD); // VT_LPSTR = string with DWORD length
      }
   };

   // If the property was not found or was not of type string, return NULL
   return NULL;
};


/**********************************************************************
** SummaryInfo::SummaryInfo()  -  Constructor
*/
SummaryInfo::SummaryInfo()
{
   m_pSumInfo=NULL;   // This IS important in case the destructor is called
                      // BEFORE m_SumInfo is first initialized or reset in Load()
};


/**********************************************************************
** SummaryInfo::~SummaryInfo()  -  Destructor
*/
SummaryInfo::~SummaryInfo(void)
{
   if (NULL!=m_pSumInfo)
      delete[] m_pSumInfo;
};

Go to TOP of page
Page last modified on 03-Mai-98
© Copyright 1998-99 homepage@vorburger.ch [E-MAIL]

Site hosted by
ItaWeb, Peruggia (Italy)

KISSfp FrontPage Add-On
KISSfp FrontPage

  URL: http://www.vorburger.ch/projects/suminfo/suminfcpp.html