home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / freethrd / server / factory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  15.0 KB  |  468 lines

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