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 / licserve / crucar.cpp next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  16.9 KB  |  504 lines

  1. /*+==========================================================================
  2.   File:      CRUCAR.CPP
  3.  
  4.   Summary:   Implementation file for the aggregatable COLicCruiseCar COM
  5.              object class.
  6.  
  7.              CRUCAR showcases the construction of the COLicCruiseCar COM
  8.              object class with the IUnknown, ICar, and ICruise interfaces.
  9.              This is done through Aggregation reuse of COCar's ICar
  10.              interface features.  COCar is provided by a separate COM
  11.              server (DLLSERVE).
  12.  
  13.              For a comprehensive tutorial code tour of this module's
  14.              contents and offerings see the tutorial DLLSERVE.HTM file.
  15.              For more specific technical details on the internal workings
  16.              see the comments dispersed throughout the module's source code.
  17.  
  18.   Classes:   COLicCruiseCar.
  19.  
  20.   Functions: none.
  21.  
  22.   Origin:    10-5-95: atrent - Editor-inheritance from CRUCAR.CPP in
  23.                the DLLSERVE Tutorial Code Sample.
  24.  
  25. ----------------------------------------------------------------------------
  26.   This file is part of the Microsoft COM Tutorial Code Samples.
  27.  
  28.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  29.  
  30.   This source code is intended only as a supplement to Microsoft
  31.   Development Tools and/or on-line documentation.  See these other
  32.   materials for detailed information regarding Microsoft code samples.
  33.  
  34.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  35.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  36.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  37.   PARTICULAR PURPOSE.
  38. ==========================================================================+*/
  39.  
  40. /*---------------------------------------------------------------------------
  41.   We include WINDOWS.H for all Win32 applications.
  42.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  43.   We include APPUTIL.H because we will be building this application using
  44.     the convenient Virtual Window and Dialog classes and other
  45.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  46.   We include ICARS.H and CARGUIDS.H for the common car-related Interface
  47.     class, GUID, and CLSID specifications.
  48.   We include SERVER.H because it has the necessary internal class and
  49.     resource definitions for this DLL.
  50.   We include CRUCAR.H because it has the class COLicCruiseCar declarations.
  51. ---------------------------------------------------------------------------*/
  52. #include <windows.h>
  53. #include <ole2.h>
  54. #include <apputil.h>
  55. #include <icars.h>
  56. #include <carguids.h>
  57. #include "server.h"
  58. #include "crucar.h"
  59.  
  60.  
  61. /*---------------------------------------------------------------------------
  62.   COLicCruiseCar's implementation of its main COM object class including
  63.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  64. ---------------------------------------------------------------------------*/
  65.  
  66. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  67.   Method:   COLicCruiseCar::COLicCruiseCar
  68.  
  69.   Summary:  COLicCruiseCar Constructor. Note the member initializer:
  70.             "m_ImpICruise(this, pUnkOuter)" which is used to pass the
  71.             'this' and pUnkOuter pointers of this constructor function
  72.             to the constructor in the instantiation of the implementation
  73.             of the CImpICruise interface (which is nested inside this
  74.             present COLicCruiseCar Object Class).
  75.  
  76.   Args:     IUnknown* pUnkOuter,
  77.               Pointer to the the outer Unknown.  NULL means this COM Object
  78.               is not being Aggregated.  Non NULL means it is being created
  79.               on behalf of an outside COM object that is reusing it via
  80.               aggregation.
  81.             CServer* pServer)
  82.               Pointer to the server's control object.
  83.  
  84.   Modifies: m_cRefs, m_pUnkOuter.
  85.  
  86.   Returns:  void
  87. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  88. COLicCruiseCar::COLicCruiseCar(
  89.   IUnknown* pUnkOuter,
  90.   CServer* pServer) :
  91.   m_ImpICruise(this, pUnkOuter)
  92. {
  93.   // Zero the COM object's reference count.
  94.   m_cRefs = 0;
  95.  
  96.   // No AddRef necessary if non-NULL, as this present COM object's lifetime
  97.   // is totally coupled with the controlling Outer object's lifetime.
  98.   m_pUnkOuter = pUnkOuter;
  99.  
  100.   // Zero the pointer to the aggregated COCar object's IUnknown
  101.   // interface (for delegation of IUnknown calls to it).
  102.   m_pUnkCar = NULL;
  103.  
  104.   // Assign the pointer to the server's control object.
  105.   m_pServer = pServer;
  106.  
  107.   LOGF1("P: COLicCruiseCar Constructor. m_pUnkOuter=0x%X.", m_pUnkOuter);
  108.  
  109.   return;
  110. }
  111.  
  112.  
  113. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  114.   Method:   COLicCruiseCar::~COLicCruiseCar
  115.  
  116.   Summary:  COLicCruiseCar Destructor.
  117.  
  118.   Args:     void
  119.  
  120.   Modifies: .
  121.  
  122.   Returns:  void
  123. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  124. COLicCruiseCar::~COLicCruiseCar(void)
  125. {
  126.   LOG("P: COLicCruiseCar::Destructor.");
  127.  
  128.   RELEASE_INTERFACE(m_pUnkCar);
  129.  
  130.   return;
  131. }
  132.  
  133.  
  134. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  135.   Method:   COLicCruiseCar::Init
  136.  
  137.   Summary:  COLicCruiseCar Initialization method.
  138.  
  139.   Args:     void
  140.  
  141.   Modifies: m_pUnkCar, m_pICar, m_cRefs.
  142.  
  143.   Returns:  HRESULT
  144.               Standard result code. NOERROR for success.
  145. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  146. HRESULT COLicCruiseCar::Init(void)
  147. {
  148.   HRESULT hr;
  149.  
  150.   LOG("P: COLicCruiseCar::Init.");
  151.  
  152.   // Set up the right pIUnknown for delegation.  If we are being
  153.   // aggregated then we pass the pUnkOuter in turn to any COM objects
  154.   // that we are aggregating.  m_pUnkOuter was set in the Constructor.
  155.   IUnknown* pUnkOuter = (NULL == m_pUnkOuter) ? this : m_pUnkOuter;
  156.  
  157.   // We create an instance of the COCar object and do this via the
  158.   // Aggregation reuse technique.  Note we pass pUnkOuter as the
  159.   // Aggregation pointer.  It is the 'this' pointer to this present
  160.   // LicCruiseCar object if we are not being aggregated; otherwise it is
  161.   // the pointer to the outermost object's controlling IUnknown.  Following
  162.   // the rules of Aggregation we must ask for an IID_IUnknown interface.
  163.   // We cache the requested  pointer to the IUnknown of the new COCar COM
  164.   // object for later use in delegating IUnknown calls.
  165.   hr = CoCreateInstance(
  166.          CLSID_DllCar,
  167.          pUnkOuter,
  168.          CLSCTX_INPROC_SERVER,
  169.          IID_IUnknown,
  170.          (PPVOID)&m_pUnkCar);
  171.  
  172.   if (SUCCEEDED(hr))
  173.   {
  174.     LOG("P: COLicCruiseCar::Init (New Aggregation of COCar) Succeeded.");
  175.   }
  176.   else
  177.   {
  178.     LOG("P: COLicCruiseCar::Init (New Aggregation of COCar) Failed.");
  179.   }
  180.  
  181.   return (hr);
  182. }
  183.  
  184.  
  185. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  186.   Method:   COLicCruiseCar::QueryInterface
  187.  
  188.   Summary:  QueryInterface of the COLicCruiseCar non-delegating
  189.             IUnknown implementation.
  190.  
  191.   Args:     REFIID riid,
  192.               [in] GUID of the Interface being requested.
  193.             PPVOID ppv)
  194.               [out] Address of the caller's pointer variable that will
  195.               receive the requested interface pointer.
  196.  
  197.   Modifies: .
  198.  
  199.   Returns:  HRESULT
  200. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  201. STDMETHODIMP COLicCruiseCar::QueryInterface(
  202.                REFIID riid,
  203.                PPVOID ppv)
  204. {
  205.   HRESULT hr = E_NOINTERFACE;
  206.   *ppv = NULL;
  207.  
  208.   if (IID_IUnknown == riid)
  209.   {
  210.     *ppv = this;
  211.     LOG("P: COLicCruiseCar::QueryInterface. 'this' pIUnknown returned.");
  212.   }
  213.   else if (IID_ICruise == riid)
  214.   {
  215.     // This ICruise interface is implemented in this COLicCruiseCar object
  216.     // and might be called a native interface of COLicCruiseCar.
  217.     *ppv = &m_ImpICruise;
  218.     LOG("P: COLicCruiseCar::QueryInterface. pICruise returned.");
  219.   }
  220.  
  221.   if (NULL != *ppv)
  222.   {
  223.     // We've handed out a pointer to an interface so obey the COM rules
  224.     //   and AddRef its reference count.
  225.     ((LPUNKNOWN)*ppv)->AddRef();
  226.     hr = NOERROR;
  227.   }
  228.   else if (IID_ICar == riid)
  229.   {
  230.     LOG("P: COLicCruiseCar::QueryInterface. ICar delegating.");
  231.     // We didn't implement the ICar interface in this COLicCruiseCar object.
  232.     // The aggregated inner COCar object is contributing the ICar interface
  233.     // to this present composite or aggregating LicCruiseCar object. So, to
  234.     // satisfy a QI request for the ICar interface, we delegate the
  235.     // QueryInterface to the inner object's principal IUnknown.
  236.     hr = m_pUnkCar->QueryInterface(riid, ppv);
  237.   }
  238.  
  239.   return (hr);
  240. }
  241.  
  242.  
  243. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  244.   Method:   COLicCruiseCar::AddRef
  245.  
  246.   Summary:  AddRef of the COLicCruiseCar non-delegating IUnknown
  247.             implementation.
  248.  
  249.   Args:     void
  250.  
  251.   Modifies: m_cRefs.
  252.  
  253.   Returns:  ULONG
  254.               New value of m_cRefs (COM object's reference count).
  255. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  256. STDMETHODIMP_(ULONG) COLicCruiseCar::AddRef(void)
  257. {
  258.   m_cRefs++;
  259.  
  260.   LOGF1("P: COLicCruiseCar::AddRef. New cRefs=%i.", m_cRefs);
  261.  
  262.   return m_cRefs;
  263. }
  264.  
  265.  
  266. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  267.   Method:   COLicCruiseCar::Release
  268.  
  269.   Summary:  Release of the COLicCruiseCar non-delegating IUnknown
  270.             implementation.
  271.  
  272.   Args:     void
  273.  
  274.   Modifies: m_cRefs.
  275.  
  276.   Returns:  ULONG
  277.               New value of m_cRefs (COM object's reference count).
  278. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  279. STDMETHODIMP_(ULONG) COLicCruiseCar::Release(void)
  280. {
  281.   ULONG ulCount = --m_cRefs;
  282.  
  283.   LOGF1("P: COLicCruiseCar::Release. New cRefs=%i.", m_cRefs);
  284.  
  285.   if (0 == m_cRefs)
  286.   {
  287.     // We've reached a zero reference count for this COM object.
  288.     // So we tell the server housing to decrement its global object
  289.     // count so that the server will be unloaded if appropriate.
  290.     if (NULL != m_pServer)
  291.       m_pServer->ObjectsDown();
  292.  
  293.     // We artificially bump the main ref count.  This fulfills one of
  294.     // the rules of aggregated objects and ensures that an indirect
  295.     // recursive call to this release won't occur because of other
  296.     // delegating releases that might happen in our own destructor.
  297.     m_cRefs++;
  298.     delete this;
  299.   }
  300.  
  301.   return ulCount;
  302. }
  303.  
  304.  
  305. /*---------------------------------------------------------------------------
  306.   COLicCruiseCar's nested implementation of the ICruise interface including
  307.   Constructor, Destructor, QueryInterface, AddRef, Release,
  308.   Engage, and Adjust.
  309. ---------------------------------------------------------------------------*/
  310.  
  311. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  312.   Method:   COLicCruiseCar::CImpICruise::CImpICruise
  313.  
  314.   Summary:  Constructor for the CImpICruise interface instantiation.
  315.  
  316.   Args:     COLicCruiseCar* pBackObj,
  317.               Back pointer to the parent outer object.
  318.             IUnknown* pUnkOuter)
  319.               Pointer to the outer Unknown.  For delegation.
  320.  
  321.   Modifies: m_cRefI, m_pBackObj, m_pUnkOuter.
  322.  
  323.   Returns:  void
  324. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  325. COLicCruiseCar::CImpICruise::CImpICruise(
  326.   COLicCruiseCar* pBackObj,
  327.   IUnknown* pUnkOuter)
  328. {
  329.   // Init the Interface Ref Count (used for debugging only).
  330.   m_cRefI = 0;
  331.  
  332.   // Init the Back Object Pointer to point to the outer object.
  333.   m_pBackObj = pBackObj;
  334.  
  335.   // Init the CImpICruise interface's delegating Unknown pointer.  We use
  336.   // the Back Object pointer for IUnknown delegation here if we are not
  337.   // being aggregated.  If we are being aggregated we use the supplied
  338.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  339.   // assignment requires no AddRef because the CImpICruise lifetime is
  340.   // quaranteed by the lifetime of the parent object in which
  341.   // CImpICruise is nested.
  342.   if (NULL == pUnkOuter)
  343.   {
  344.     m_pUnkOuter = pBackObj;
  345.     LOG("P: COLicCruiseCar::CImpICruise Constructor. Non-Aggregating.");
  346.   }
  347.   else
  348.   {
  349.     m_pUnkOuter = pUnkOuter;
  350.     LOG("P: COLicCruiseCar::CImpICruise Constructor. Aggregating.");
  351.   }
  352.  
  353.   return;
  354. }
  355.  
  356.  
  357. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  358.   Method:   COLicCruiseCar::CImpICruise::~CImpICruise
  359.  
  360.   Summary:  Destructor for the CImpICruise interface instantiation.
  361.  
  362.   Args:     void
  363.  
  364.   Modifies: .
  365.  
  366.   Returns:  void
  367. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  368. COLicCruiseCar::CImpICruise::~CImpICruise(void)
  369. {
  370.   LOG("P: COLicCruiseCar::CImpICruise Destructor.");
  371.  
  372.   return;
  373. }
  374.  
  375.  
  376. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  377.   Method:   COLicCruiseCar::CImpICruise::QueryInterface
  378.  
  379.   Summary:  The QueryInterface IUnknown member of this ICruise interface
  380.             implementation that delegates to m_pUnkOuter, whatever it is.
  381.  
  382.   Args:     REFIID riid,
  383.               [in] GUID of the Interface being requested.
  384.             PPVOID ppv)
  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.               Returned by the delegated outer QueryInterface call.
  392. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  393. STDMETHODIMP COLicCruiseCar::CImpICruise::QueryInterface(
  394.                REFIID riid,
  395.                PPVOID ppv)
  396. {
  397.   LOG("P: COLicCruiseCar::CImpICruise::QueryInterface. Delegating.");
  398.  
  399.   // Delegate this call to the outer object's QueryInterface.
  400.   return m_pUnkOuter->QueryInterface(riid, ppv);
  401. }
  402.  
  403.  
  404. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  405.   Method:   COLicCruiseCar::CImpICruise::AddRef
  406.  
  407.   Summary:  The AddRef IUnknown member of this ICruise interface
  408.             implementation that delegates to m_pUnkOuter, whatever it is.
  409.  
  410.   Args:     void
  411.  
  412.   Modifies: m_cRefI.
  413.  
  414.   Returns:  ULONG
  415.               Returned by the delegated outer AddRef call.
  416. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  417. STDMETHODIMP_(ULONG) COLicCruiseCar::CImpICruise::AddRef(void)
  418. {
  419.   // Increment the Interface Reference Count.
  420.   ++m_cRefI;
  421.  
  422.   LOGF1("P: COLicCruiseCar::CImpICruise::Addref. Delegating. New cI=%i.", m_cRefI);
  423.  
  424.   // Delegate this call to the outer object's AddRef.
  425.   return m_pUnkOuter->AddRef();
  426. }
  427.  
  428.  
  429. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  430.   Method:   COLicCruiseCar::CImpICruise::Release
  431.  
  432.   Summary:  The Release IUnknown member of this ICruise interface
  433.             implementation that delegates to m_pUnkOuter, whatever it is.
  434.  
  435.   Args:     void
  436.  
  437.   Modifies: .
  438.  
  439.   Returns:  ULONG
  440.               Returned by the delegated outer Release call.
  441. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  442. STDMETHODIMP_(ULONG) COLicCruiseCar::CImpICruise::Release(void)
  443. {
  444.   // Decrement the Interface Reference Count.
  445.   --m_cRefI;
  446.  
  447.   LOGF1("P: COLicCruiseCar::CImpICruise::Release. Delegating. New cI=%i.",m_cRefI);
  448.  
  449.   // Delegate this call to the outer object's Release.
  450.   return m_pUnkOuter->Release();
  451. }
  452.  
  453.  
  454. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  455.   Method:   COLicCruiseCar::CImpICruise::Engage
  456.  
  457.   Summary:  The Engage member method of this ICruise interface
  458.             implementation.  A simple empty method on a COLicCruiseCar COM
  459.             object for tutorial purposes.  Presumably if this Car object
  460.             were modeling a real Car then the Engage method would turn
  461.             the Cruise control system on or off.
  462.  
  463.   Args:     BOOL bOnOff)
  464.               TRUE for On; FALSE for Off.
  465.  
  466.   Modifies: .
  467.  
  468.   Returns:  HRESULT
  469.               NOERROR
  470. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  471. STDMETHODIMP COLicCruiseCar::CImpICruise::Engage(
  472.                BOOL bOnOff)
  473. {
  474.   LOGF1("P: COLicCruiseCar::CImpICruise::Engage. Called. bOnOff=%i.",bOnOff);
  475.  
  476.   return NOERROR;
  477. }
  478.  
  479.  
  480. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  481.   Method:   COLicCruiseCar::CImpICruise::Adjust
  482.  
  483.   Summary:  The Adjust member method of this ICruise interface
  484.             implementation.  A simple empty method on a COLicCruiseCar COM
  485.             object for tutorial purposes.  Presumably if this Car object
  486.             were modeling a real Car then the Adjust method would allow
  487.             notching the cruise set speed up or down by increments of 3 mph.
  488.  
  489.   Args:     BOOL bUpDown)
  490.               TRUE for Up; FALSE for Down.
  491.  
  492.   Modifies: .
  493.  
  494.   Returns:  HRESULT
  495.               NOERROR
  496. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  497. STDMETHODIMP COLicCruiseCar::CImpICruise::Adjust(
  498.                BOOL bUpDown)
  499. {
  500.   LOGF1("P: COLicCruiseCar::CImpICruise::Adjust. Called. bUpDown=%i.",bUpDown);
  501.  
  502.   return NOERROR;
  503. }
  504.