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 / freserve / factory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-30  |  15.1 KB  |  459 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 tutorial FRESERVE.HTM
  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 Tutorial Code Sample.
  23.  
  24. ----------------------------------------------------------------------------
  25.   This file is part of the Microsoft COM Tutorial Code Samples.
  26.  
  27.   Copyright (C) Microsoft Corporation, 1997.  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 calling the COM/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 <windows.h>
  56. #include <ole2.h>
  57. #include <apputil.h>
  58. #include <iball.h>
  59. #include <ballguid.h>
  60. #include "server.h"
  61. #include "factory.h"
  62. #include "ball.h"
  63.  
  64. /*---------------------------------------------------------------------------
  65.   Implementation the CFBall Class Factory.  CFBall is the COM
  66.   object class for the Class Factory that can manufacture COBall
  67.   COM Components.
  68. ---------------------------------------------------------------------------*/
  69.  
  70. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  71.   Method:   CFBall::CFBall
  72.  
  73.   Summary:  CFBall Constructor. Note the member initializer:
  74.             "m_ImpIClassFactory(this, pUnkOuter, pServer)" which is used
  75.             to pass the 'this', pUnkOuter, and pServer pointers of this
  76.             constructor function to the constructor executed in the
  77.             instantiation of the CImpIClassFactory interface whose
  78.             implementation is nested inside this present object class.
  79.  
  80.   Args:     IUnknown* pUnkOuter,
  81.               Pointer to the the outer Unknown.  NULL means this COM Object
  82.               is not being Aggregated.  Non NULL means it is being created
  83.               on behalf of an outside COM object that is reusing it via
  84.               aggregation.
  85.             CServer* pServer)
  86.               Pointer to the server's control object.
  87.  
  88.   Modifies: m_cRefs, m_pUnkOuter.
  89.  
  90.   Returns:  void
  91. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  92. CFBall::CFBall(
  93.   IUnknown* pUnkOuter,
  94.   CServer* pServer) :
  95.   m_ImpIClassFactory(this, pUnkOuter, pServer)
  96. {
  97.   // Zero the COM object's reference count.
  98.   m_cRefs = 0;
  99.  
  100.   // No AddRef necessary if non-NULL, as we're nested.
  101.   m_pUnkOuter = pUnkOuter;
  102.  
  103.   // Init the pointer to the server control object.
  104.   m_pServer = pServer;
  105.  
  106.   return;
  107. }
  108.  
  109.  
  110. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  111.   Method:   CFBall::~CFBall
  112.  
  113.   Summary:  CFBall Destructor.
  114.  
  115.   Args:     void
  116.  
  117.   Returns:  void
  118. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  119. CFBall::~CFBall(void)
  120. {
  121.   return;
  122. }
  123.  
  124.  
  125. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  126.   Method:   CFBall::QueryInterface
  127.  
  128.   Summary:  QueryInterface of the CFBall non-delegating
  129.             IUnknown implementation.
  130.  
  131.   Args:     REFIID riid,
  132.               [in] GUID of the Interface being requested.
  133.             PPVOID ppv)
  134.               [out] Address of the caller's pointer variable that will
  135.               receive the requested interface pointer.
  136.  
  137.   Returns:  HRESULT
  138.               Standard result code. NOERROR for success.
  139. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  140. STDMETHODIMP CFBall::QueryInterface(
  141.                REFIID riid,
  142.                PPVOID ppv)
  143. {
  144.   HRESULT hr = E_NOINTERFACE;
  145.  
  146.   if (OwnThis())
  147.   {
  148.     *ppv = NULL;
  149.  
  150.     if (IID_IUnknown == riid)
  151.       *ppv = this;
  152.     else if (IID_IClassFactory == riid)
  153.       *ppv = &m_ImpIClassFactory;
  154.  
  155.     if (NULL != *ppv)
  156.     {
  157.       // We've handed out a pointer to the interface so obey the COM rules
  158.       // and AddRef the reference count.
  159.       ((LPUNKNOWN)*ppv)->AddRef();
  160.       hr = NOERROR;
  161.     }
  162.  
  163.     UnOwnThis();
  164.   }
  165.  
  166.   return (hr);
  167. }
  168.  
  169.  
  170. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  171.   Method:   CFBall::AddRef
  172.  
  173.   Summary:  AddRef of the CFBall non-delegating IUnknown implementation.
  174.  
  175.   Args:     void
  176.  
  177.   Modifies: m_cRefs.
  178.  
  179.   Returns:  ULONG
  180.               New value of m_cRefs (COM object's reference count).
  181. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  182. STDMETHODIMP_(ULONG) CFBall::AddRef(void)
  183. {
  184.   ULONG cRefs;
  185.  
  186.   if (OwnThis())
  187.   {
  188.     cRefs = ++m_cRefs;
  189.  
  190.     UnOwnThis();
  191.   }
  192.  
  193.   return cRefs;
  194. }
  195.  
  196.  
  197. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  198.   Method:   CFBall::Release
  199.  
  200.   Summary:  Release of the CFBall non-delegating IUnknown implementation.
  201.  
  202.   Args:     void
  203.  
  204.   Modifies: m_cRefs.
  205.  
  206.   Returns:  ULONG
  207.               New value of m_cRefs (COM object's reference count).
  208. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  209. STDMETHODIMP_(ULONG) CFBall::Release(void)
  210. {
  211.   ULONG cRefs;
  212.  
  213.   if (OwnThis())
  214.   {
  215.     cRefs = --m_cRefs;
  216.  
  217.     if (0 == cRefs)
  218.     {
  219.       // We artificially bump the main ref count to prevent reentrancy
  220.       // via the main object destructor.  Not really needed in this
  221.       // CFBall but a good practice because we are aggregatable and
  222.       // may at some point in the future add something entertaining like
  223.       // some Releases to the CFBall destructor.
  224.       m_cRefs++;
  225.       UnOwnThis();
  226.       delete this;
  227.  
  228.       // We've reached a zero reference count for this COM object.
  229.       // So we tell the server housing to decrement its global object
  230.       // count so that the server will be unloaded if appropriate.
  231.       if (NULL != m_pServer)
  232.         m_pServer->ObjectsDown();
  233.     }
  234.     else
  235.       UnOwnThis();
  236.   }
  237.  
  238.   return cRefs;
  239. }
  240.  
  241.  
  242. /*---------------------------------------------------------------------------
  243.   CFBall's nested implementation of the IClassFactory interface
  244.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  245.   CreateInstance, and LockServer methods.
  246. ---------------------------------------------------------------------------*/
  247.  
  248. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  249.   Method:   CFBall::CImpIClassFactory::CImpIClassFactory
  250.  
  251.   Summary:  Constructor for the CImpIClassFactory interface instantiation.
  252.  
  253.   Args:     CFBall* pBackObj,
  254.               Back pointer to the parent outer object.
  255.             IUnknown* pUnkOuter,
  256.               Pointer to the outer Unknown.  For delegation.
  257.             CServer* pServer)
  258.               Pointer to the server's control object.
  259.  
  260.   Modifies: m_pBackObj, m_pUnkOuter, m_pServer.
  261.  
  262.   Returns:  void
  263. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  264. CFBall::CImpIClassFactory::CImpIClassFactory(
  265.   CFBall* pBackObj,
  266.   IUnknown* pUnkOuter,
  267.   CServer* pServer)
  268. {
  269.   // Init the Back Object Pointer to point to the parent object.
  270.   m_pBackObj = pBackObj;
  271.  
  272.   // Init the pointer to the server control object.
  273.   m_pServer = pServer;
  274.  
  275.   // Init the CImpIClassFactory interface's delegating Unknown pointer.
  276.   // We use the Back Object pointer for IUnknown delegation here if we are
  277.   // not being aggregated.  If we are being aggregated we use the supplied
  278.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  279.   // assignment requires no AddRef because the CImpIClassFactory lifetime is
  280.   // quaranteed by the lifetime of the parent object in which
  281.   // CImpIClassFactory is nested.
  282.   if (NULL == pUnkOuter)
  283.     m_pUnkOuter = pBackObj;
  284.   else
  285.     m_pUnkOuter = pUnkOuter;
  286.  
  287.   return;
  288. }
  289.  
  290.  
  291. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  292.   Method:   CFBall::CImpIClassFactory::~CImpIClassFactory
  293.  
  294.   Summary:  Destructor for the CImpIClassFactory interface instantiation.
  295.  
  296.   Args:     void
  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.   Returns:  HRESULT
  320.               Standard result code. NOERROR for success.
  321.               Returned by the delegated outer QueryInterface call.
  322. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  323. STDMETHODIMP CFBall::CImpIClassFactory::QueryInterface(
  324.                REFIID riid,
  325.                PPVOID ppv)
  326. {
  327.   // Delegate this call to the outer object's QueryInterface.
  328.   return m_pUnkOuter->QueryInterface(riid, ppv);
  329. }
  330.  
  331.  
  332. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  333.   Method:   CFBall::CImpIClassFactory::AddRef
  334.  
  335.   Summary:  The AddRef IUnknown member of this IClassFactory interface
  336.             implementation that delegates to m_pUnkOuter, whatever it is.
  337.  
  338.   Args:     void
  339.  
  340.   Returns:  ULONG
  341.               Returned by the delegated outer AddRef call.
  342. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  343. STDMETHODIMP_(ULONG) CFBall::CImpIClassFactory::AddRef(void)
  344. {
  345.   // Delegate this call to the outer object's AddRef.
  346.   return m_pUnkOuter->AddRef();
  347. }
  348.  
  349.  
  350. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  351.   Method:   CFBall::CImpIClassFactory::Release
  352.  
  353.   Summary:  The Release IUnknown member of this IClassFactory interface
  354.             implementation that delegates to m_pUnkOuter, whatever it is.
  355.  
  356.   Args:     void
  357.  
  358.   Returns:  ULONG
  359.               Returned by the delegated outer Release call.
  360. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  361. STDMETHODIMP_(ULONG) CFBall::CImpIClassFactory::Release(void)
  362. {
  363.   // Delegate this call to the outer object's Release.
  364.   return m_pUnkOuter->Release();
  365. }
  366.  
  367.  
  368. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  369.   Method:   CFBall::CImpIClassFactory::CreateInstance
  370.  
  371.   Summary:  The CreateInstance member method of this IClassFactory
  372.             interface implementation.  Creates an instance of the COBall
  373.             COM component.
  374.  
  375.   Args:     IUnknown* pUnkOuter,
  376.               [in] Pointer to the controlling IUnknown.
  377.             REFIID riid,
  378.               [in] GUID of the Interface being requested.
  379.             PPVOID ppvCob)
  380.               [out] Address of the caller's pointer variable that will
  381.               receive the requested interface pointer.
  382.  
  383.   Returns:  HRESULT
  384.               Standard result code. NOERROR for success.
  385. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  386. STDMETHODIMP CFBall::CImpIClassFactory::CreateInstance(
  387.                IUnknown* pUnkOuter,
  388.                REFIID riid,
  389.                PPVOID ppv)
  390. {
  391.   HRESULT hr = E_FAIL;
  392.   COBall* pCob = NULL;
  393.  
  394.   // NULL the output pointer.
  395.   *ppv = NULL;
  396.  
  397.   // If the creation call is requesting aggregation (pUnkOuter != NULL),
  398.   // the COM rules state the IUnknown interface MUST also be concomitantly
  399.   // be requested.  If it is not so requested (riid != IID_IUnknown) then
  400.   // an error must be returned indicating that no aggregate creation of
  401.   // the CFBall COM Object can be performed.
  402.   if (NULL != pUnkOuter && riid != IID_IUnknown)
  403.     hr = CLASS_E_NOAGGREGATION;
  404.   else
  405.   {
  406.     // Instantiate a COBall COM Object.
  407.     pCob = new COBall(pUnkOuter, m_pServer);
  408.     if (NULL != pCob)
  409.     {
  410.       // We initially created the new COM object so tell the server
  411.       // to increment its global server object count to help ensure
  412.       // that the server remains loaded until this partial creation
  413.       // of a COM component is completed.
  414.       m_pServer->ObjectsUp();
  415.  
  416.       // We QueryInterface this new COM Object not only to deposit the
  417.       // main interface pointer to the caller's pointer variable, but to
  418.       // also automatically bump the Reference Count on the new COM
  419.       // Object after handing out this reference to it.
  420.       hr = pCob->QueryInterface(riid, (PPVOID)ppv);
  421.       if (FAILED(hr))
  422.       {
  423.         m_pServer->ObjectsDown();
  424.         delete pCob;
  425.       }
  426.     }
  427.     else
  428.       hr = E_OUTOFMEMORY;
  429.   }
  430.  
  431.   return hr;
  432. }
  433.  
  434.  
  435. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  436.   Method:   CFBall::CImpIClassFactory::LockServer
  437.  
  438.   Summary:  The LockServer member method of this IClassFactory interface
  439.             implementation.
  440.  
  441.   Args:     BOOL fLock)
  442.               [in] Flag determining whether to Lock or Unlock the server.
  443.  
  444.   Returns:  HRESULT
  445.               Standard result code. NOERROR for success.
  446. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  447. STDMETHODIMP CFBall::CImpIClassFactory::LockServer(
  448.                BOOL fLock)
  449. {
  450.   HRESULT hr = NOERROR;
  451.  
  452.   if (fLock)
  453.     m_pServer->Lock();
  454.   else
  455.     m_pServer->Unlock();
  456.  
  457.   return hr;
  458. }
  459.