home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap09 / linksrc / idescrip.cpp < prev    next >
C/C++ Source or Header  |  1996-05-21  |  3KB  |  142 lines

  1. /*
  2.  * IDESCRIP.CPP
  3.  * Link Source Chapter 9
  4.  *
  5.  * Implementation the IDescription interface that doesn't care
  6.  * what object it works with in this LinkSource sample.  It
  7.  * is given a storage in which it expects to find a stream
  8.  * called "Description" containing the text relevant to the
  9.  * object showing the instance of this interface.
  10.  *
  11.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  12.  *
  13.  * Kraig Brockschmidt, Microsoft
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include "linksrc.h"
  20.  
  21. /*
  22.  * CImpIDescription:CImpIDescription
  23.  * CImpIDescription::~CImpIDescription
  24.  * CImpIDescription::QueryInterface
  25.  * CImpIDescription::AddRef
  26.  * CImpIDescription::Release
  27.  *
  28.  * Basic object members.
  29.  */
  30.  
  31. CImpIDescription::CImpIDescription(LPUNKNOWN pUnkOuter)
  32.     {
  33.     m_cRef=0;
  34.     m_pUnkOuter=pUnkOuter;
  35.     m_pIStorage=NULL;
  36.     return;
  37.     }
  38.  
  39. CImpIDescription::~CImpIDescription(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44. STDMETHODIMP CImpIDescription::QueryInterface(REFIID riid
  45.     , LPVOID *ppv)
  46.     {
  47.     return m_pUnkOuter->QueryInterface(riid, ppv);
  48.     }
  49.  
  50. STDMETHODIMP_(ULONG) CImpIDescription::AddRef(void)
  51.     {
  52.     ++m_cRef;
  53.     return m_pUnkOuter->AddRef();
  54.     }
  55.  
  56. STDMETHODIMP_(ULONG) CImpIDescription::Release(void)
  57.     {
  58.     --m_cRef;
  59.     return m_pUnkOuter->Release();
  60.     }
  61.  
  62.  
  63. /*
  64.  * CImpIDescription::GetText
  65.  *
  66.  * Purpose:
  67.  *  Fills a buffer with our text description.
  68.  *
  69.  * Parameters:
  70.  *  pszText         LPOLESTR to the buffer to fill
  71.  *  cch             ULONG specifying the length of pszText
  72.  *
  73.  * Return Value:
  74.  *  HRESULT         NOERROR if successful, error otherwise.
  75.  */
  76.  
  77. HRESULT CImpIDescription::GetText(LPOLESTR pszText, ULONG cch)
  78.     {
  79.     HRESULT     hr;
  80.     IStream    *pIStream;
  81.  
  82.     /*
  83.      * The description text for this object is contained in
  84.      * a stream called "Description" (constant SZDESCRIPTION
  85.      * has this string) found in whatever storage we happen
  86.      * to have.  This implementation is ignorant of the actual
  87.      * object that is exposing it.
  88.      */
  89.  
  90.     if (NULL==m_pIStorage)
  91.         return ResultFromScode(E_FAIL);
  92.  
  93.     hr=m_pIStorage->OpenStream(SZDESCRIPTION, 0, STGM_DIRECT
  94.         | STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  95.  
  96.     if (FAILED(hr))
  97.         return ResultFromScode(E_FAIL);
  98.  
  99.    #ifdef WIN32ANSI
  100.     char sz[512];
  101.     hr=pIStream->Read((void *)sz, cch*sizeof(TCHAR), NULL);    
  102.     MultiByteToWideChar(CP_ACP, 0, sz, -1, pszText, cch);    
  103.    #else
  104.     hr=pIStream->Read((void *)pszText, cch*sizeof(WCHAR), NULL);
  105.    #endif
  106.     pIStream->Release();
  107.  
  108.     return SUCCEEDED(hr) ? NOERROR : ResultFromScode(E_FAIL);
  109.     }
  110.  
  111.  
  112.  
  113.  
  114. /*
  115.  * (Internal)
  116.  * CImpIDescription::SetStorage
  117.  *
  118.  * Purpose:
  119.  *  Provides objects using this interface implementation to
  120.  *  inform us of the storage in which to look for the
  121.  *  description stream.
  122.  *
  123.  * Parameters:
  124.  *  pIStorage       IStorage * to hold.
  125.  *
  126.  * Return Value:
  127.  *  None
  128.  */
  129.  
  130. void CImpIDescription::SetStorage(IStorage *pIStorage)
  131.     {
  132.     /*
  133.      * This may be the root storage for the File object, the
  134.      * sub-storage for the first level object, or a sub-storage
  135.      * for a second-level object.  Since the object exposing
  136.      * this interface holds the storage itself, no AddRef is
  137.      * required.
  138.      */
  139.     m_pIStorage=pIStorage;
  140.     return;
  141.     }
  142.