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 / chap24 / patron / iclassf.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  3KB  |  168 lines

  1. /*
  2.  * ICLASSF.CPP
  3.  * Patron Chapter 24
  4.  *
  5.  * Implementation of an IClassFactory interface for Patron to
  6.  * enable linking to its embedded objects.
  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 "patron.h"
  17.  
  18.  
  19. /*
  20.  * CLinkClassFactory::CLinkClassFactory
  21.  * CLinkClassFactory::~CLinkClassFactory
  22.  *
  23.  * Constructor Parameters:
  24.  *  pFR             PCLinkFrame that can create documents.
  25.  */
  26.  
  27. CLinkClassFactory::CLinkClassFactory(PCPatronFrame pFR)
  28.     {
  29.     m_cRef=0L;
  30.     m_pFR=pFR;
  31.     return;
  32.     }
  33.  
  34.  
  35. CLinkClassFactory::~CLinkClassFactory(void)
  36.     {
  37.     return;
  38.     }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CLinkClassFactory::QueryInterface
  47.  * CLinkClassFactory::AddRef
  48.  * CLinkClassFactory::Release
  49.  */
  50.  
  51. STDMETHODIMP CLinkClassFactory::QueryInterface(REFIID riid
  52.     , PPVOID ppv)
  53.     {
  54.     *ppv=NULL;
  55.  
  56.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  57.         *ppv=this;
  58.  
  59.     if (NULL!=*ppv)
  60.         {
  61.         ((LPUNKNOWN)*ppv)->AddRef();
  62.         return NOERROR;
  63.         }
  64.  
  65.     return ResultFromScode(E_NOINTERFACE);
  66.     }
  67.  
  68.  
  69. STDMETHODIMP_(ULONG) CLinkClassFactory::AddRef(void)
  70.     {
  71.     return ++m_cRef;
  72.     }
  73.  
  74.  
  75. STDMETHODIMP_(ULONG) CLinkClassFactory::Release(void)
  76.     {
  77.     if (0!=--m_cRef)
  78.         return m_cRef;
  79.  
  80.     delete this;
  81.     return 0;
  82.     }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /*
  91.  * CLinkClassFactory::CreateInstance
  92.  *
  93.  * Purpose:
  94.  *  Instantiates an object to resolve a link to an embedding.
  95.  */
  96.  
  97. STDMETHODIMP CLinkClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
  98.     , REFIID riid, PPVOID ppvObj)
  99.     {
  100.     PCPatronDoc         pDoc;
  101.     HRESULT             hr;
  102.  
  103.     *ppvObj=NULL;
  104.  
  105.     hr=ResultFromScode(E_OUTOFMEMORY);
  106.  
  107.     //We don't support aggregation
  108.     if (NULL!=pUnkOuter)
  109.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  110.  
  111.     //Try creating a new document, which creates the object.
  112.     pDoc=(PCPatronDoc)m_pFR->m_pCL->NewDocument(TRUE);
  113.  
  114.     //ObjectDestroyed has already been called.
  115.     if (NULL==pDoc)
  116.         {
  117.         g_cObj++;
  118.         ObjectDestroyed();
  119.         return hr;
  120.         }
  121.  
  122.     /*
  123.      * We don't want to do any file initialization here because we
  124.      * want to wait for IPersistFile calls.  If we called Load(NULL)
  125.      * we'd create storages and whatnot that we don't want in this
  126.      * case.
  127.      */
  128.  
  129.     hr=pDoc->QueryInterface(riid, ppvObj);
  130.  
  131.     if (FAILED(hr))
  132.         {
  133.         m_pFR->m_pCL->CloseDocument(pDoc);
  134.         g_cObj++;
  135.         ObjectDestroyed();
  136.         return hr;
  137.         }
  138.  
  139.     m_pFR->m_pDocCreated=pDoc;
  140.     return NOERROR;
  141.     }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * CLinkClassFactory::LockServer
  150.  *
  151.  * Purpose:
  152.  *  Provides control of the application in memory.
  153.  */
  154.  
  155. STDMETHODIMP CLinkClassFactory::LockServer(BOOL fLock)
  156.     {
  157.     if (fLock)
  158.         g_cLock++;
  159.     else
  160.         {
  161.         g_cLock--;
  162.         g_cObj++;
  163.         ObjectDestroyed();
  164.         }
  165.  
  166.     return NOERROR;
  167.     }
  168.