home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / dcdserve / factory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-30  |  16.3 KB  |  494 lines

  1. /*+==========================================================================
  2.   File:      FACTORY.CPP
  3.  
  4.   Summary:   Implementation file for the class factories of the DCDSERVE
  5.              server.  This server provides the SharePaper COM component.
  6.              For this component, IClassFactory is implemented in an
  7.              appropriate class factory COM object: CFPaper. The COM
  8.              component that can be manufactured by this server is known
  9.              outside the server by its CLSID: CLSID_SharePaper.
  10.  
  11.              The class factories in this server use the CThreaded
  12.              OwnThis mechanism to ensure mutually exclusive access by
  13.              contending multiple threads.
  14.  
  15.              For a comprehensive tutorial code tour of this module's
  16.              contents and offerings see the tutorial DCDSERVE.HTM
  17.              file. For more specific technical details on the internal
  18.              workings see the comments dispersed throughout the module's
  19.              source code.
  20.  
  21.   Classes:   CFPaper.
  22.  
  23.   Functions: .
  24.  
  25.   Origin:    8-24-97: atrent - Editor-inheritance from FACTORY.CPP in
  26.                the LOCSERVE Tutorial Code Sample. [Revised]
  27.  
  28. ----------------------------------------------------------------------------
  29.   This file is part of the Microsoft COM Tutorial Code Samples.
  30.  
  31.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  32.  
  33.   This source code is intended only as a supplement to Microsoft
  34.   Development Tools and/or on-line documentation.  See these other
  35.   materials for detailed information regarding Microsoft code samples.
  36.  
  37.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  38.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  39.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  40.   PARTICULAR PURPOSE.
  41. ==========================================================================+*/
  42.  
  43. /*---------------------------------------------------------------------------
  44.   We include WINDOWS.H for all Win32 applications.
  45.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  46.   We include OLECTL.H because it has definitions for connectable objects.
  47.   We include APPUTIL.H because we will be building this EXE using
  48.     the convenient Virtual Window and Dialog classes and other
  49.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  50.   We include PAPINT.H and PAPGUIDS.H for the common Paper-related
  51.     Interface class, GUID, and CLSID specifications. PAPINT.H is generated
  52.     in the DCDMARSH marshaling sample.
  53.   We include SERVER.H because it has the necessary internal class and
  54.     resource definitions for this EXE.
  55.   We include FACTORY.H because it has the necessary internal class factory
  56.     declarations for this EXE component server.  Those factories we will be
  57.     implementing in this module.
  58.   We include CONNECT.H for object class declarations for the various
  59.     connection point and connection COM objects used in DCDSERVE.
  60.   We include PAPER.H, for the object class declarations for the
  61.     COPaper COM object.
  62. ---------------------------------------------------------------------------*/
  63. #include <windows.h>
  64. #include <ole2.h>
  65. #include <olectl.h>
  66. #include <apputil.h>
  67. #include <papint.h>
  68. #include <papguids.h>
  69. #include "server.h"
  70. #include "factory.h"
  71. #include "connect.h"
  72. #include "paper.h"
  73.  
  74. /*---------------------------------------------------------------------------
  75.   Implementation the CFPaper Class Factory.  CFPaper is the COM
  76.   object class for the Class Factory that can manufacture COPaper
  77.   COM Components.
  78. ---------------------------------------------------------------------------*/
  79.  
  80. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  81.   Method:   CFPaper::CFPaper
  82.  
  83.   Summary:  CFPaper Constructor. Note the member initializer:
  84.             "m_ImpIClassFactory(this, pUnkOuter, pServer)" which is used
  85.             to pass the 'this', pUnkOuter, and pServer pointers of this
  86.             constructor function to the constructor executed in the
  87.             instantiation of the CImpIClassFactory interface whose
  88.             implementation is nested inside this present object class.
  89.  
  90.   Args:     IUnknown* pUnkOuter,
  91.               Pointer to the the outer Unknown.  NULL means this COM Object
  92.               is not being Aggregated.  Non NULL means it is being created
  93.               on behalf of an outside COM object that is reusing it via
  94.               aggregation.
  95.             CServer* pServer)
  96.               Pointer to the server's control object.
  97.  
  98.   Modifies: m_cRefs, m_pUnkOuter.
  99.  
  100.   Returns:  void
  101. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  102. CFPaper::CFPaper(
  103.   IUnknown* pUnkOuter,
  104.   CServer* pServer) :
  105.   m_ImpIClassFactory(this, pUnkOuter, pServer)
  106. {
  107.   // Zero the COM object's reference count.
  108.   m_cRefs = 0;
  109.  
  110.   // No AddRef necessary if non-NULL, as we're nested.
  111.   m_pUnkOuter = pUnkOuter;
  112.  
  113.   // Init the pointer to the server control object.
  114.   m_pServer = pServer;
  115.  
  116.   return;
  117. }
  118.  
  119.  
  120. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  121.   Method:   CFPaper::~CFPaper
  122.  
  123.   Summary:  CFPaper Destructor.
  124.  
  125.   Args:     void
  126.  
  127.   Returns:  void
  128. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  129. CFPaper::~CFPaper(void)
  130. {
  131.   return;
  132. }
  133.  
  134.  
  135. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  136.   Method:   CFPaper::QueryInterface
  137.  
  138.   Summary:  QueryInterface of the CFPaper non-delegating
  139.             IUnknown implementation.
  140.  
  141.   Args:     REFIID riid,
  142.               [in] GUID of the Interface being requested.
  143.             PPVOID ppv)
  144.               [out] Address of the caller's pointer variable that will
  145.               receive the requested interface pointer.
  146.  
  147.   Returns:  HRESULT
  148.               Standard result code. NOERROR for success.
  149. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  150. STDMETHODIMP CFPaper::QueryInterface(
  151.                REFIID riid,
  152.                PPVOID ppv)
  153. {
  154.   HRESULT hr = E_POINTER;
  155.  
  156.   if (OwnThis())
  157.   {
  158.     if (NULL != ppv)
  159.     {
  160.       hr = E_NOINTERFACE;
  161.       *ppv = NULL;
  162.  
  163.       if (IID_IUnknown == riid)
  164.         *ppv = this;
  165.       else if (IID_IClassFactory == riid)
  166.         *ppv = &m_ImpIClassFactory;
  167.  
  168.       if (NULL != *ppv)
  169.       {
  170.         // We've handed out a pointer to the interface so obey the COM
  171.         // rules and AddRef the reference count.
  172.         ((LPUNKNOWN)*ppv)->AddRef();
  173.         hr = NOERROR;
  174.       }
  175.     }
  176.  
  177.     UnOwnThis();
  178.   }
  179.  
  180.   return (hr);
  181. }
  182.  
  183.  
  184. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  185.   Method:   CFPaper::AddRef
  186.  
  187.   Summary:  AddRef of the CFPaper non-delegating IUnknown implementation.
  188.  
  189.   Args:     void
  190.  
  191.   Modifies: m_cRefs.
  192.  
  193.   Returns:  ULONG
  194.               New value of m_cRefs (COM object's reference count).
  195. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  196. STDMETHODIMP_(ULONG) CFPaper::AddRef(void)
  197. {
  198.   ULONG cRefs;
  199.  
  200.   if (OwnThis())
  201.   {
  202.     cRefs = ++m_cRefs;
  203.  
  204.     UnOwnThis();
  205.   }
  206.  
  207.   return cRefs;
  208. }
  209.  
  210.  
  211. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  212.   Method:   CFPaper::Release
  213.  
  214.   Summary:  Release of the CFPaper non-delegating IUnknown implementation.
  215.  
  216.   Args:     void
  217.  
  218.   Modifies: m_cRefs.
  219.  
  220.   Returns:  ULONG
  221.               New value of m_cRefs (COM object's reference count).
  222. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  223. STDMETHODIMP_(ULONG) CFPaper::Release(void)
  224. {
  225.   ULONG cRefs;
  226.  
  227.   if (OwnThis())
  228.   {
  229.     cRefs = --m_cRefs;
  230.  
  231.     if (0 == cRefs)
  232.     {
  233.       // We've reached a zero reference count for this COM object.
  234.       // So we tell the server housing to decrement its global object
  235.       // count so that the server will be unloaded if appropriate.
  236.       if (NULL != m_pServer)
  237.         m_pServer->ObjectsDown();
  238.  
  239.       // We artificially bump the main ref count to prevent reentrancy
  240.       // via the main object destructor.  Not really needed in this
  241.       // CFPaper but a good practice because we are aggregatable and
  242.       // may at some point in the future add something entertaining like
  243.       // some Releases to the CFPaper destructor.
  244.       m_cRefs++;
  245.       UnOwnThis();
  246.       delete this;
  247.     }
  248.     else
  249.       UnOwnThis();
  250.   }
  251.  
  252.   return cRefs;
  253. }
  254.  
  255.  
  256. /*---------------------------------------------------------------------------
  257.   CFPaper's nested implementation of the IClassFactory interface
  258.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  259.   CreateInstance, and LockServer methods.
  260. ---------------------------------------------------------------------------*/
  261.  
  262. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  263.   Method:   CFPaper::CImpIClassFactory::CImpIClassFactory
  264.  
  265.   Summary:  Constructor for the CImpIClassFactory interface instantiation.
  266.  
  267.   Args:     CFPaper* pCO,
  268.               Back pointer to the parent outer object.
  269.             IUnknown* pUnkOuter,
  270.               Pointer to the outer Unknown.  For delegation.
  271.             CServer* pServer)
  272.               Pointer to the server's control object.
  273.  
  274.   Modifies: m_pCO, m_pUnkOuter, m_pServer.
  275.  
  276.   Returns:  void
  277. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  278. CFPaper::CImpIClassFactory::CImpIClassFactory(
  279.   CFPaper* pCO,
  280.   IUnknown* pUnkOuter,
  281.   CServer* pServer)
  282. {
  283.   // Init the Back Object Pointer to point to the parent object.
  284.   m_pCO = pCO;
  285.  
  286.   // Init the pointer to the server control object.
  287.   m_pServer = pServer;
  288.  
  289.   // Init the CImpIClassFactory interface's delegating Unknown pointer.
  290.   // We use the Back Object pointer for IUnknown delegation here if we are
  291.   // not being aggregated. If we are being aggregated we use the supplied
  292.   // pUnkOuter for IUnknown delegation. In either case the pointer
  293.   // assignment requires no AddRef because the CImpIClassFactory lifetime
  294.   // is quaranteed by the lifetime of the parent object in which
  295.   // CImpIClassFactory is nested.
  296.   if (NULL == pUnkOuter)
  297.     m_pUnkOuter = pCO;
  298.   else
  299.     m_pUnkOuter = pUnkOuter;
  300.  
  301.   return;
  302. }
  303.  
  304.  
  305. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  306.   Method:   CFPaper::CImpIClassFactory::~CImpIClassFactory
  307.  
  308.   Summary:  Destructor for the CImpIClassFactory interface instantiation.
  309.  
  310.   Args:     void
  311.  
  312.   Returns:  void
  313. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  314. CFPaper::CImpIClassFactory::~CImpIClassFactory(void)
  315. {
  316.   return;
  317. }
  318.  
  319.  
  320. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  321.   Method:   CFPaper::CImpIClassFactory::QueryInterface
  322.  
  323.   Summary:  The QueryInterface IUnknown member of this IClassFactory
  324.             interface implementation that delegates to m_pUnkOuter,
  325.             whatever it is.
  326.  
  327.   Args:     REFIID riid,
  328.               [in] GUID of the Interface being requested.
  329.             PPVOID ppv)
  330.               [out] Address of the caller's pointer variable that will
  331.               receive the requested interface pointer.
  332.  
  333.   Returns:  HRESULT
  334.               Standard result code. NOERROR for success.
  335.               Returned by the delegated outer QueryInterface call.
  336. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  337. STDMETHODIMP CFPaper::CImpIClassFactory::QueryInterface(
  338.                REFIID riid,
  339.                PPVOID ppv)
  340. {
  341.   // Delegate this call to the outer object's QueryInterface.
  342.   return m_pUnkOuter->QueryInterface(riid, ppv);
  343. }
  344.  
  345.  
  346. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  347.   Method:   CFPaper::CImpIClassFactory::AddRef
  348.  
  349.   Summary:  The AddRef IUnknown member of this IClassFactory interface
  350.             implementation that delegates to m_pUnkOuter, whatever it is.
  351.  
  352.   Args:     void
  353.  
  354.   Returns:  ULONG
  355.               Returned by the delegated outer AddRef call.
  356. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  357. STDMETHODIMP_(ULONG) CFPaper::CImpIClassFactory::AddRef(void)
  358. {
  359.   // Delegate this call to the outer object's AddRef.
  360.   return m_pUnkOuter->AddRef();
  361. }
  362.  
  363.  
  364. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  365.   Method:   CFPaper::CImpIClassFactory::Release
  366.  
  367.   Summary:  The Release IUnknown member of this IClassFactory interface
  368.             implementation that delegates to m_pUnkOuter, whatever it is.
  369.  
  370.   Args:     void
  371.  
  372.   Returns:  ULONG
  373.               Returned by the delegated outer Release call.
  374. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  375. STDMETHODIMP_(ULONG) CFPaper::CImpIClassFactory::Release(void)
  376. {
  377.   // Delegate this call to the outer object's Release.
  378.   return m_pUnkOuter->Release();
  379. }
  380.  
  381.  
  382. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  383.   Method:   CFPaper::CImpIClassFactory::CreateInstance
  384.  
  385.   Summary:  The CreateInstance member method of this IClassFactory
  386.             interface implementation.  Creates an instance of the COPaper
  387.             COM component.
  388.  
  389.   Args:     IUnknown* pUnkOuter,
  390.               [in] Pointer to the controlling IUnknown.
  391.             REFIID riid,
  392.               [in] GUID of the Interface being requested.
  393.             PPVOID ppvCob)
  394.               [out] Address of the caller's pointer variable that will
  395.               receive the requested interface pointer.
  396.  
  397.   Returns:  HRESULT
  398.               Standard result code. NOERROR for success.
  399. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  400. STDMETHODIMP CFPaper::CImpIClassFactory::CreateInstance(
  401.                IUnknown* pUnkOuter,
  402.                REFIID riid,
  403.                PPVOID ppv)
  404. {
  405.   HRESULT hr = E_POINTER;
  406.   COPaper* pCob = NULL;
  407.  
  408.   if (NULL != ppv)
  409.   {
  410.     // NULL the output pointer.
  411.     *ppv = NULL;
  412.     hr = E_FAIL;
  413.  
  414.     // If the creation call is requesting aggregation (pUnkOuter != NULL),
  415.     // the COM rules state the IUnknown interface MUST also be concomitantly
  416.     // be requested.  If it is not so requested (riid != IID_IUnknown) then
  417.     // an error must be returned indicating that no aggregate creation of
  418.     // the CFPaper COM Object can be performed.
  419.     if (NULL != pUnkOuter && riid != IID_IUnknown)
  420.       hr = CLASS_E_NOAGGREGATION;
  421.     else
  422.     {
  423.       if (m_pServer->ObjectFirst())
  424.       {
  425.         // Instantiate a COPaper COM Object.
  426.         pCob = new COPaper(pUnkOuter, m_pServer);
  427.         if (NULL != pCob)
  428.         {
  429.           // Create and initialize any subordinate objects.
  430.           hr = pCob->Init();
  431.           if (SUCCEEDED(hr))
  432.           {
  433.             // We successfully created the new COM object so tell the
  434.             // server to increment its global server object count.
  435.             m_pServer->ObjectsUp();
  436.  
  437.             // We QueryInterface this new COM Object not only to deposit the
  438.             // main interface pointer to the caller's pointer variable, but
  439.             // to also automatically bump the Reference Count on the new COM
  440.             // Object after handing out this reference to it.
  441.             hr = pCob->QueryInterface(riid, (PPVOID)ppv);
  442.             if (SUCCEEDED(hr))
  443.             {
  444.               // Rig so that any subsequent creates get the same shared
  445.               // single object.
  446.               m_pServer->ObjectSet(pCob);
  447.             }
  448.             else
  449.             {
  450.               delete pCob;
  451.               m_pServer->ObjectsDown();
  452.             }
  453.  
  454.           }
  455.           else
  456.             delete pCob;
  457.         }
  458.         else
  459.           hr = E_OUTOFMEMORY;
  460.       }
  461.       else
  462.         hr = m_pServer->ObjectQI(riid, ppv);
  463.     }
  464.   }
  465.  
  466.   return hr;
  467. }
  468.  
  469.  
  470. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  471.   Method:   CFPaper::CImpIClassFactory::LockServer
  472.  
  473.   Summary:  The LockServer member method of this IClassFactory interface
  474.             implementation.
  475.  
  476.   Args:     BOOL bLock)
  477.               [in] Flag determining whether to Lock or Unlock the server.
  478.  
  479.   Returns:  HRESULT
  480.               Standard result code. NOERROR for success.
  481. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  482. STDMETHODIMP CFPaper::CImpIClassFactory::LockServer(
  483.                BOOL bLock)
  484. {
  485.   HRESULT hr = NOERROR;
  486.  
  487.   if (bLock)
  488.     m_pServer->Lock();
  489.   else
  490.     m_pServer->Unlock();
  491.  
  492.   return hr;
  493. }
  494.