ICLASSF.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*
  2.  * ICLASSF.CPP
  3.  * Cosmo Chapter 23
  4.  *
  5.  * Implementation of Cosmo's class factory.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13. #include "cosmo.h"
  14. /*
  15.  * CFigureClassFactory::CFigureClassFactory
  16.  * CFigureClassFactory::~CFigureClassFactory
  17.  *
  18.  * Constructor Parameters:
  19.  *  pFR             PCCosmoFrame that can create documents.
  20.  */
  21. CFigureClassFactory::CFigureClassFactory(PCCosmoFrame pFR)
  22.     {
  23.     m_cRef=0L;
  24.     m_pFR=pFR;
  25.     m_fCreated=FALSE;
  26.     return;
  27.     }
  28. CFigureClassFactory::~CFigureClassFactory(void)
  29.     {
  30.     return;
  31.     }
  32. /*
  33.  * CFigureClassFactory::QueryInterface
  34.  * CFigureClassFactory::AddRef
  35.  * CFigureClassFactory::Release
  36.  */
  37. STDMETHODIMP CFigureClassFactory::QueryInterface(REFIID riid
  38.     , PPVOID ppv)
  39.     {
  40.     *ppv=NULL;
  41.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  42.         *ppv=this;
  43.     if (NULL!=*ppv)
  44.         {
  45.         ((LPUNKNOWN)*ppv)->AddRef();
  46.         return NOERROR;
  47.         }
  48.     return ResultFromScode(E_NOINTERFACE);
  49.     }
  50. STDMETHODIMP_(ULONG) CFigureClassFactory::AddRef(void)
  51.     {
  52.     return ++m_cRef;
  53.     }
  54. STDMETHODIMP_(ULONG) CFigureClassFactory::Release(void)
  55.     {
  56.     if (0!=--m_cRef)
  57.         return m_cRef;
  58.     delete this;
  59.     return 0;
  60.     }
  61. /*
  62.  * CFigureClassFactory::CreateInstance
  63.  *
  64.  * Purpose:
  65.  *  Instantiates a Figure object that supports embedding.
  66.  *
  67.  * Parameters:
  68.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
  69.  *                  being used in an aggregation.
  70.  *  riid            REFIID identifying the interface the caller
  71.  *                  desires to have for the new object.
  72.  *  ppvObj          PPVOID in which to store the desired
  73.  *                  interface pointer for the new object.
  74.  *
  75.  * Return Value:
  76.  *  HRESULT         NOERROR if successful, otherwise contains
  77.  *                  E_NOINTERFACE if we cannot support the requested
  78.  *                  interface.
  79.  */
  80. STDMETHODIMP CFigureClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
  81.     , REFIID riid, PPVOID ppvObj)
  82.     {
  83.     PCCosmoDoc          pDoc;
  84.     HRESULT             hr;
  85.     *ppvObj=NULL;
  86.     //Great idea to protect yourself from multiple creates here.
  87.     if (m_fCreated)
  88.         return ResultFromScode(E_UNEXPECTED);
  89.     m_fCreated=TRUE;
  90.     hr=ResultFromScode(E_OUTOFMEMORY);
  91.     //We don't support aggregation
  92.     if (NULL!=pUnkOuter)
  93.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  94.     //Try creating a new document, which creates the object.
  95.     pDoc=(PCCosmoDoc)m_pFR->m_pCL->NewDocument(TRUE);
  96.     if (NULL==pDoc)
  97.         {
  98.         //This will cause shutdown; object count will go to zero.
  99.         g_cObj++;
  100.         ObjectDestroyed();
  101.         return hr;
  102.         }
  103.     //Insure the document is untitled; get the requested interface.
  104.     pDoc->Load(TRUE, NULL);
  105.     pDoc->m_pFigure->FrameSet(m_pFR);
  106.     hr=pDoc->m_pFigure->QueryInterface(riid, ppvObj);
  107.     //Closing the document will destroy the objec, cause shutdown.
  108.     if (FAILED(hr))
  109.         {
  110.         m_pFR->m_pCL->CloseDocument(pDoc);
  111.         return hr;
  112.         }
  113.     return NOERROR;
  114.     }
  115. /*
  116.  * CFigureClassFactory::LockServer
  117.  *
  118.  * Purpose:
  119.  *  Increments or decrements the lock count of the serving
  120.  *  IClassFactory object.  When the number of locks goes to
  121.  *  zero and the number of objects is zero, we shut down the
  122.  *  application.
  123.  *
  124.  * Parameters:
  125.  *  fLock           BOOL specifying whether to increment or
  126.  *                  decrement the lock count.
  127.  *
  128.  * Return Value:
  129.  *  HRESULT         NOERROR always.
  130.  */
  131. STDMETHODIMP CFigureClassFactory::LockServer(BOOL fLock)
  132.     {
  133.     if (fLock)
  134.         g_cLock++;
  135.     else
  136.         {
  137.         g_cLock--;
  138.         g_cObj++;
  139.         ObjectDestroyed();
  140.         }
  141.     return NOERROR;
  142.     }