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 / contitem.cpp next >
C/C++ Source or Header  |  1995-05-03  |  5KB  |  208 lines

  1. /*
  2.  * CONTITEM.CPP
  3.  * Container Item Object for Link Source, Chapter 9
  4.  *
  5.  * Implementation of an item object that is itself a container
  6.  * of other items.  It implements IDescription and
  7.  * IOleItemContainer using the shared implemenations in
  8.  * IDESCRIP.CPP and IOLECONT.CPP
  9.  *
  10.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  11.  *
  12.  * Kraig Brockschmidt, Microsoft
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "linksrc.h"
  19.  
  20.  
  21. #ifdef WIN32ANSI
  22. /*
  23.  * This is to turn off the mapping to ANSI wrapper APIs because
  24.  * we're actually using wide char strings under Win32 all the time
  25.  * in parts of this code.
  26.  */
  27. #undef CreateItemMoniker
  28. #define CreateItemMoniker CreateItemMoniker
  29.  
  30. #endif
  31.  
  32.  
  33. /*
  34.  * CContainerItem::CContainerItem
  35.  * CContainerItem::~CContainerItem
  36.  *
  37.  * Parameters (Constructor):
  38.  *  pUnkParent      LPUNKNOWN to the parent in which this
  39.  *                  item lives
  40.  *  pfnDestroy      PFNDESTROYED to call when an object
  41.  *                  is destroyed.
  42.  */
  43.  
  44. CContainerItem::CContainerItem(LPUNKNOWN pUnkParent
  45.     , PFNDESTROYED pfnDestroy)
  46.     {
  47.     m_cRef=0;
  48.  
  49.     //This keeps the parent storage open as well
  50.     m_pUnkParent=pUnkParent;
  51.     pUnkParent->AddRef();
  52.  
  53.     m_pfnDestroy=pfnDestroy;
  54.  
  55.     m_pIStorage=NULL;
  56.     m_pmk=NULL;
  57.     m_dwRegROT=0;
  58.  
  59.     m_pImpIOleItemContainer=NULL;
  60.     m_pImpIDescription=NULL;
  61.  
  62.     return;
  63.     }
  64.  
  65. CContainerItem::~CContainerItem(void)
  66.     {
  67.     //Remove us from the running object table
  68.     if (0!=m_dwRegROT)
  69.         {
  70.         IRunningObjectTable    *pROT;
  71.  
  72.         if (SUCCEEDED(GetRunningObjectTable(0, &pROT)))
  73.             {
  74.             pROT->Revoke(m_dwRegROT);
  75.             pROT->Release();
  76.             }
  77.         }
  78.  
  79.     ReleaseInterface(m_pmk);
  80.     ReleaseInterface(m_pIStorage);
  81.     DeleteInterfaceImp(m_pImpIDescription);
  82.     DeleteInterfaceImp(m_pImpIOleItemContainer);
  83.  
  84.     ReleaseInterface(m_pUnkParent);
  85.     return;
  86.     }
  87.  
  88.  
  89.  
  90. /*
  91.  * CContainerItem::Init
  92.  *
  93.  * Purpose:
  94.  *  Performs any intiailization of a CContainerItem that's prone to
  95.  *  failure that we also use internally before exposing the object
  96.  *  outside.
  97.  *
  98.  * Parameters:
  99.  *  pmkLeft         IMoniker * of our containing object.
  100.  *  pbc             IBindCtx * to use to register ourselves
  101.  *                  as running.
  102.  *  pszItem         LPOLESTR naming this object.
  103.  *  pIStorage       IStorage * to our information.
  104.  *
  105.  * Return Value:
  106.  *  BOOL            TRUE if the function is successful,
  107.  *                  FALSE otherwise.
  108.  */
  109.  
  110. BOOL CContainerItem::Init(IMoniker *pmkLeft, IBindCtx *pbc
  111.     , LPOLESTR pszItem, IStorage *pIStorage)
  112.     {
  113.     OLECHAR     szDelim[]=OLETEXT("!");
  114.     HRESULT     hr=ResultFromScode(S_FALSE);
  115.     IMoniker   *pmkItem;
  116.  
  117.     m_pImpIOleItemContainer=new CImpIOleItemContainer(this, this
  118.         , FALSE);
  119.  
  120.     if (NULL==m_pImpIOleItemContainer)
  121.         return FALSE;
  122.  
  123.     m_pImpIDescription=new CImpIDescription(this);
  124.  
  125.     if (NULL==m_pImpIDescription)
  126.         return FALSE;
  127.  
  128.     m_pIStorage=pIStorage;
  129.     m_pImpIDescription->SetStorage(m_pIStorage);
  130.  
  131.  
  132.     /*
  133.      * Create an item moniker for ourselves and register as
  134.      * running.  Failure here is not critical.
  135.      */
  136.     if (FAILED(CreateItemMoniker(szDelim, pszItem, &pmkItem)))
  137.         return TRUE;
  138.  
  139.     //Create a composite for ourselves
  140.     if (SUCCEEDED(pmkLeft->ComposeWith(pmkItem, FALSE, &m_pmk)))
  141.         {
  142.         IRunningObjectTable *pROT;
  143.  
  144.         if (SUCCEEDED(pbc->GetRunningObjectTable(&pROT)))
  145.             {
  146.             pROT->Register(0, this, m_pmk, &m_dwRegROT);
  147.             pROT->Release();
  148.             }
  149.         }
  150.  
  151.     pmkItem->Release();
  152.     return TRUE;
  153.     }
  154.  
  155.  
  156.  
  157.  
  158. /*
  159.  * CContainerItem::QueryInterface
  160.  * CContainerItem::AddRef
  161.  * CContainerItem::Release
  162.  *
  163.  * Purpose:
  164.  *  IUnknown members for CContainerItem object.
  165.  */
  166.  
  167. STDMETHODIMP CContainerItem::QueryInterface(REFIID riid, PPVOID ppv)
  168.     {
  169.     *ppv=NULL;
  170.  
  171.     if (IID_IUnknown==riid)
  172.         *ppv=this;
  173.  
  174.     if (IID_IParseDisplayName==riid || IID_IOleContainer==riid
  175.         || IID_IOleItemContainer==riid)
  176.         *ppv=m_pImpIOleItemContainer;
  177.  
  178.     if (IID_IDescription==riid)
  179.         *ppv=m_pImpIDescription;
  180.  
  181.     if (NULL!=*ppv)
  182.         {
  183.         ((LPUNKNOWN)*ppv)->AddRef();
  184.         return NOERROR;
  185.         }
  186.  
  187.     return ResultFromScode(E_NOINTERFACE);
  188.     }
  189.  
  190.  
  191. STDMETHODIMP_(ULONG) CContainerItem::AddRef(void)
  192.     {
  193.     return ++m_cRef;
  194.     }
  195.  
  196.  
  197. STDMETHODIMP_(ULONG) CContainerItem::Release(void)
  198.     {
  199.     if (0L!=--m_cRef)
  200.         return m_cRef;
  201.  
  202.     if (NULL!=m_pfnDestroy)
  203.         (*m_pfnDestroy)();
  204.  
  205.     delete this;
  206.     return 0;
  207.     }
  208.