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 / simpitem.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  192 lines

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