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 / locserve / utilcar.cpp < prev    next >
C/C++ Source or Header  |  1997-08-05  |  25KB  |  764 lines

  1. /*+==========================================================================
  2.   File:      UTILCAR.CPP
  3.  
  4.   Summary:   Implementation file for the aggregatable COUtilityCar COM
  5.              object class.
  6.  
  7.              UTILCAR showcases the construction of the COUtilityCar COM
  8.              object class with the IUnknown, ICar, and IUtility interfaces.
  9.              This is done through Containment reuse of COCar's ICar
  10.              interface features.
  11.  
  12.              For a comprehensive tutorial code tour of this module's
  13.              contents and offerings see the tutorial LOCSERVE.HTM file.
  14.              For more specific technical details on the internal workings
  15.              see the comments dispersed throughout the module's source code.
  16.  
  17.   Classes:   COUtilityCar.
  18.  
  19.   Functions: none.
  20.  
  21.   Origin:    11-14-95: atrent - Editor-inheritance from UTILCAR.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 application using
  43.     the convenient Virtual Window and Dialog classes and other
  44.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  45.   We include MICARS.H and CARGUIDS.H for the common car-related Interface
  46.     class, GUID, and CLSID specifications.
  47.   We include SERVER.H because it has internal class declarations and
  48.     resource ID definitions specific for this DLL.
  49.   We include CAR.H because it has the class COCar declarations.
  50.   We include UTILCAR.H because it has the class COUtilityCar declarations.
  51. ---------------------------------------------------------------------------*/
  52. #include <windows.h>
  53. #include <ole2.h>
  54. #include <apputil.h>
  55. #include <micars.h>
  56. #include <carguids.h>
  57. #include "server.h"
  58. #include "car.h"
  59. #include "utilcar.h"
  60.  
  61.  
  62. /*---------------------------------------------------------------------------
  63.   COUtilityCar's implementation of its main COM object class including
  64.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  65. ---------------------------------------------------------------------------*/
  66.  
  67. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  68.   Method:   COUtilityCar::COUtilityCar
  69.  
  70.   Summary:  COUtilityCar Constructor. Note the member initializers:
  71.             "m_ImpICar(this, pUnkOuter)" and "m_ImpIUtility(this,
  72.             pUnkOuter)" which are used to pass the 'this' and
  73.             pUnkOuter pointers of this constructor function to the
  74.             constructors that instantiate the implementations of
  75.             the CImpICar and CImpIUtility interfaces (which are both
  76.             nested inside this present COUtilityCar Object Class.
  77.  
  78.   Args:     IUnknown* pUnkOuter,
  79.               Pointer to the the outer Unknown.  NULL means this COM Object
  80.               is not being Aggregated.  Non NULL means it is being created
  81.               on behalf of an outside COM object that is reusing it via
  82.               aggregation.
  83.             CServer* pServer)
  84.               Pointer to the server's control object.
  85.  
  86.   Modifies: m_cRefs, m_pUnkOuter.
  87.  
  88.   Returns:  void
  89. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  90. COUtilityCar::COUtilityCar(
  91.   IUnknown* pUnkOuter,
  92.   CServer* pServer) :
  93.   m_ImpICar(this, pUnkOuter),
  94.   m_ImpIUtility(this, pUnkOuter)
  95. {
  96.   // Zero the COM object's reference count.
  97.   m_cRefs = 0;
  98.  
  99.   // No AddRef necessary if non-NULL, as we're nested.
  100.   m_pUnkOuter = pUnkOuter;
  101.  
  102.   // Zero the pointer to the contained COCar object's ICar interface.
  103.   m_pICar = NULL;
  104.  
  105.   // Init the pointer to the server's control object.
  106.   m_pServer = pServer;
  107.  
  108.   LOGF1("L: COUtilityCar Constructor. m_pUnkOuter=0x%X.", m_pUnkOuter);
  109.  
  110.   return;
  111. }
  112.  
  113.  
  114. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  115.   Method:   COUtilityCar::~COUtilityCar
  116.  
  117.   Summary:  COUtilityCar Destructor.
  118.  
  119.   Args:     void
  120.  
  121.   Modifies: .
  122.  
  123.   Returns:  void
  124. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  125. COUtilityCar::~COUtilityCar(void)
  126. {
  127.   LOG("L: COUtilityCar::Destructor.");
  128.  
  129.   // Release the contained Car object.
  130.   m_pICar->Release();
  131.  
  132.   return;
  133. }
  134.  
  135.  
  136. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  137.   Method:   COUtilityCar::Init
  138.  
  139.   Summary:  COUtilityCar Initialization method.
  140.  
  141.   Args:     void
  142.  
  143.   Modifies: m_pUnkCar, m_pICar, m_cRefs.
  144.  
  145.   Returns:  HRESULT
  146.               Standard result code. NOERROR for success.
  147. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  148. HRESULT COUtilityCar::Init(void)
  149. {
  150.   HRESULT hr;
  151.  
  152.   LOG("L: COUtilityCar::Init.");
  153.  
  154.   // Set up the right pIUnknown for delegation.  If we are being
  155.   // aggregated then we pass the pUnkOuter in turn to any COM objects
  156.   // that we are aggregating.  m_pUnkOuter was set in the Constructor.
  157.   IUnknown* pUnkOuter = (NULL == m_pUnkOuter) ? this : m_pUnkOuter;
  158.  
  159.   // We create an instance of the COCar object and do this via the
  160.   // Containment reuse technique.  We ask for the new COM object's
  161.   // ICar interface directly.  We pass NULL for the pUnkOuter
  162.   // aggregation pointer because we are not aggregating.  It is here
  163.   // that we are reusing the COCar COM Object through the Containment
  164.   // technique.  We cache the requested ICar interface pointer in this
  165.   // COUtilityCar COM object for later use.  We don't need to AddRef
  166.   // this interface because the CoCreateInstance will do this for us.
  167.   hr = CoCreateInstance(
  168.          CLSID_LocCar,
  169.          NULL,
  170.          CLSCTX_LOCAL_SERVER,
  171.          IID_ICar,
  172.          (PPVOID)&m_pICar);
  173.  
  174.   if (SUCCEEDED(hr))
  175.   {
  176.     LOG("L: COUtilityCar::Init (New Containment of COCar) Succeeded.");
  177.   }
  178.   else
  179.   {
  180.     LOG("L: COUtilityCar::Init (New Containment of COCar) Failed.");
  181.   }
  182.  
  183.   return (hr);
  184. }
  185.  
  186.  
  187. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  188.   Method:   COUtilityCar::QueryInterface
  189.  
  190.   Summary:  QueryInterface of the COUtilityCar non-delegating
  191.             IUnknown implementation.
  192.  
  193.   Args:     REFIID riid,
  194.               [in] GUID of the Interface being requested.
  195.             PPVOID ppv)
  196.               [out] Address of the caller's pointer variable that will
  197.               receive the requested interface pointer.
  198.  
  199.   Modifies: .
  200.  
  201.   Returns:  HRESULT
  202. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  203. STDMETHODIMP COUtilityCar::QueryInterface(
  204.                REFIID riid,
  205.                PPVOID ppv)
  206. {
  207.   HRESULT hr = E_NOINTERFACE;
  208.   *ppv = NULL;
  209.  
  210.   if (IID_IUnknown == riid)
  211.   {
  212.     *ppv = this;
  213.     LOG("L: COUtilityCar::QueryInterface. 'this' pIUnknown returned");
  214.   }
  215.   else if (IID_ICar == riid)
  216.   {
  217.     *ppv = &m_ImpICar;
  218.     LOG("L: COUtilityCar::QueryInterface. pICar returned");
  219.   }
  220.   else if (IID_IUtility == riid)
  221.   {
  222.     *ppv = &m_ImpIUtility;
  223.     LOG("L: COUtilityCar::QueryInterface. pIUtility returned");
  224.   }
  225.  
  226.   if (NULL != *ppv)
  227.   {
  228.     // We've handed out a pointer to the interface so obey the COM rules
  229.     // and AddRef the reference count.
  230.     ((LPUNKNOWN)*ppv)->AddRef();
  231.     hr = NOERROR;
  232.   }
  233.  
  234.   return (hr);
  235. }
  236.  
  237.  
  238. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  239.   Method:   COUtilityCar::AddRef
  240.  
  241.   Summary:  AddRef of the COUtilityCar non-delegating IUnknown implementation.
  242.  
  243.   Args:     void
  244.  
  245.   Modifies: m_cRefs.
  246.  
  247.   Returns:  ULONG
  248.               New value of m_cRefs (COM object's reference count).
  249. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  250. STDMETHODIMP_(ULONG) COUtilityCar::AddRef(void)
  251. {
  252.   m_cRefs++;
  253.  
  254.   LOGF1("L: COUtilityCar::AddRef. New cRefs=%i.", m_cRefs);
  255.  
  256.   return m_cRefs;
  257. }
  258.  
  259.  
  260. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  261.   Method:   COUtilityCar::Release
  262.  
  263.   Summary:  Release of the COUtilityCar non-delegating IUnknown implementation.
  264.  
  265.   Args:     void
  266.  
  267.   Modifies: m_cRefs.
  268.  
  269.   Returns:  ULONG
  270.               New value of m_cRefs (COM object's reference count).
  271. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  272. STDMETHODIMP_(ULONG) COUtilityCar::Release(void)
  273. {
  274.   m_cRefs--;
  275.  
  276.   LOGF1("L: COUtilityCar::Release. New cRefs=%i.", m_cRefs);
  277.  
  278.   if (0 == m_cRefs)
  279.   {
  280.     // We've reached a zero reference count for this COM object.
  281.     // So we tell the server housing to decrement its global object
  282.     // count so that the server will be unloaded if appropriate.
  283.     if (NULL != m_pServer)
  284.       m_pServer->ObjectsDown();
  285.  
  286.     // We artificially bump the main ref count to prevent reentrancy
  287.     // via the main object destructor.
  288.     m_cRefs++;
  289.     delete this;
  290.   }
  291.  
  292.   return m_cRefs;
  293. }
  294.  
  295.  
  296. /*---------------------------------------------------------------------------
  297.   COUtilityCar's nested implementation of the ICar interface including
  298.   Constructor, Destructor, QueryInterface, AddRef, Release,
  299.   Shift, Clutch, Speed, and Steer.
  300. ---------------------------------------------------------------------------*/
  301.  
  302. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  303.   Method:   COUtilityCar::CImpICar::CImpICar
  304.  
  305.   Summary:  Constructor for the CImpICar interface instantiation.
  306.  
  307.   Args:     COUtilityCar* pBackObj,
  308.               Back pointer to the parent outer object.
  309.             IUnknown* pUnkOuter,
  310.               Pointer to the outer Unknown.  For delegation.
  311.  
  312.   Modifies: m_cRefI, m_pBackObj, m_pUnkOuter.
  313.  
  314.   Returns:  void
  315. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  316. COUtilityCar::CImpICar::CImpICar(
  317.   COUtilityCar* pBackObj,
  318.   IUnknown* pUnkOuter)
  319. {
  320.   // Init the Interface Ref Count (used for debugging only).
  321.   m_cRefI = 0;
  322.  
  323.   // Init the Back Object Pointer to point to the outer object.
  324.   m_pBackObj = pBackObj;
  325.  
  326.   // Init the CImpICar interface's delegating Unknown pointer.  We use
  327.   // the Back Object pointer for IUnknown delegation here if we are not
  328.   // being aggregated.  If we are being aggregated we use the supplied
  329.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  330.   // assignment requires no AddRef because the CImpICar lifetime is
  331.   // quaranteed by the lifetime of the parent object in which
  332.   // CImpICar is nested.
  333.   if (NULL == pUnkOuter)
  334.   {
  335.     m_pUnkOuter = pBackObj;
  336.     LOG("L: COUtilityCar::CImpICar Constructor. Non-Aggregating");
  337.   }
  338.   else
  339.   {
  340.     m_pUnkOuter = pUnkOuter;
  341.     LOG("L: COUtilityCar::CImpICar Constructor. Aggregating");
  342.   }
  343.  
  344.   return;
  345. }
  346.  
  347.  
  348. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  349.   Method:   COUtilityCar::CImpICar::~CImpICar
  350.  
  351.   Summary:  Destructor for the CImpICar interface instantiation.
  352.  
  353.   Args:     void
  354.  
  355.   Modifies: .
  356.  
  357.   Returns:  void
  358. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  359. COUtilityCar::CImpICar::~CImpICar(void)
  360. {
  361.   LOG("L: COUtilityCar::CImpICar Destructor.");
  362.  
  363.   return;
  364. }
  365.  
  366.  
  367. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  368.   Method:   COUtilityCar::CImpICar::QueryInterface
  369.  
  370.   Summary:  The QueryInterface IUnknown member of this ICar interface
  371.             implementation that delegates to m_pUnkOuter, whatever it is.
  372.  
  373.   Args:     REFIID riid,
  374.               [in] GUID of the Interface being requested.
  375.             PPVOID ppv)
  376.               [out] Address of the caller's pointer variable that will
  377.               receive the requested interface pointer.
  378.  
  379.   Modifies: .
  380.  
  381.   Returns:  HRESULT
  382.               Returned by the delegated outer QueryInterface call.
  383. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  384. STDMETHODIMP COUtilityCar::CImpICar::QueryInterface(
  385.                REFIID riid,
  386.                PPVOID ppv)
  387. {
  388.   LOG("L: COUtilityCar::CImpICar::QueryInterface. Delegating.");
  389.  
  390.   // Delegate this call to the outer object's QueryInterface.
  391.   return m_pUnkOuter->QueryInterface(riid, ppv);
  392. }
  393.  
  394.  
  395. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  396.   Method:   COUtilityCar::CImpICar::AddRef
  397.  
  398.   Summary:  The AddRef IUnknown member of this ICar interface
  399.             implementation that delegates to m_pUnkOuter, whatever it is.
  400.  
  401.   Args:     void
  402.  
  403.   Modifies: m_cRefI.
  404.  
  405.   Returns:  ULONG
  406.               Returned by the delegated outer AddRef call.
  407. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  408. STDMETHODIMP_(ULONG) COUtilityCar::CImpICar::AddRef(void)
  409. {
  410.   // Increment the Interface Reference Count.
  411.   ++m_cRefI;
  412.  
  413.   LOGF1("L: COUtilityCar::CImpICar::Addref. Delegating. New cI=%i.", m_cRefI);
  414.  
  415.   // Delegate this call to the outer object's AddRef.
  416.   return m_pUnkOuter->AddRef();
  417. }
  418.  
  419.  
  420. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  421.   Method:   COUtilityCar::CImpICar::Release
  422.  
  423.   Summary:  The Release IUnknown member of this ICar interface
  424.             implementation that delegates to m_pUnkOuter, whatever it is.
  425.  
  426.   Args:     void
  427.  
  428.   Modifies: .
  429.  
  430.   Returns:  ULONG
  431.               Returned by the delegated outer Release call.
  432. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  433. STDMETHODIMP_(ULONG) COUtilityCar::CImpICar::Release(void)
  434. {
  435.   // Decrement the Interface Reference Count.
  436.   --m_cRefI;
  437.  
  438.   LOGF1("L: COUtilityCar::CImpICar::Release. Delegating. New cI=%i.", m_cRefI);
  439.  
  440.   // Delegate this call to the outer object's Release.
  441.   return m_pUnkOuter->Release();
  442. }
  443.  
  444.  
  445. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  446.   Method:   COUtilityCar::CImpICar::Shift
  447.  
  448.   Summary:  The Shift member method of this ICar interface implementation.
  449.             A simple empty method on a COUtilityCar COM object for tutorial
  450.             purposes.  Presumably if this Car object were modeling
  451.             a real Car then the Shift method would shift to the specified
  452.             gear.
  453.  
  454.   Args:     short nGear
  455.               0 - Neutral; 1 - 5 First 5 forward gears; 6 - Reverse.
  456.  
  457.   Modifies: .
  458.  
  459.   Returns:  HRESULT
  460.               NOERROR
  461. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  462. STDMETHODIMP COUtilityCar::CImpICar::Shift(
  463.                short nGear)
  464. {
  465.   LOGF1("L: COUtilityCar::CImpICar::Shift. Delegating. nGear=%i.",nGear);
  466.  
  467.   // Delegate to the contained object implementation of ICar.
  468.   m_pBackObj->m_pICar->Shift(nGear);
  469.  
  470.   return NOERROR;
  471. }
  472.  
  473.  
  474. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  475.   Method:   COUtilityCar::CImpICar::Clutch
  476.  
  477.   Summary:  The Clutch member method of this ICar interface implementation.
  478.             A simple empty method on a COUtilityCar COM object for tutorial
  479.             purposes.  Presumably if this Car object were modeling
  480.             a real Car then the Clutch method would engage the clutch the
  481.             specified amount.
  482.  
  483.   Args:     short nEngaged)
  484.               Percent clutch is engaged (0 to 100%).
  485.  
  486.   Modifies: .
  487.  
  488.   Returns:  HRESULT
  489.               NOERROR
  490. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  491. STDMETHODIMP COUtilityCar::CImpICar::Clutch(
  492.                short nEngaged)
  493. {
  494.   LOGF1("L: COUtilityCar::CImpICar::Clutch. Delegating. nEngaged=%i.", nEngaged);
  495.  
  496.   // Delegate to the contained object implementation of ICar.
  497.   m_pBackObj->m_pICar->Clutch(nEngaged);
  498.  
  499.   return NOERROR;
  500. }
  501.  
  502.  
  503. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  504.   Method:   COUtilityCar::CImpICar::Speed
  505.  
  506.   Summary:  The Propel member method of this ICar interface implementation.
  507.             A simple empty method on a COUtilityCar COM object for tutorial
  508.             purposes.  Presumably if this Car object were modeling
  509.             a real Car then this method would accelerate/brake to bring
  510.             the car to the specified speed.
  511.  
  512.   Args:     short nMph
  513.               New speed in miles per hour.
  514.  
  515.   Modifies: .
  516.  
  517.   Returns:  HRESULT
  518.               NOERROR
  519. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  520. STDMETHODIMP COUtilityCar::CImpICar::Speed(
  521.                short nMph)
  522. {
  523.   LOGF1("L: COUtilityCar::CImpICar::Speed. Delegating. nMph=%i.",nMph);
  524.  
  525.   // Delegate to the contained object implementation of ICar.
  526.   m_pBackObj->m_pICar->Speed(nMph);
  527.  
  528.   return NOERROR;
  529. }
  530.  
  531.  
  532. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  533.   Method:   COUtilityCar::CImpICar::Steer
  534.  
  535.   Summary:  The Steer member method of this ICar interface implementation.
  536.             A simple empty method on a COUtilityCar COM object for tutorial
  537.             purposes.  Presumably if this Car object were modeling
  538.             a real Car then the Steer method would set the steering
  539.             angle of the Car.
  540.  
  541.   Args:     short nAngle)
  542.               0 degrees straight ahead, -45 Full left, +45 Full right.
  543.  
  544.   Modifies: .
  545.  
  546.   Returns:  HRESULT
  547.               NOERROR
  548. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  549. STDMETHODIMP COUtilityCar::CImpICar::Steer(
  550.                short nAngle)
  551. {
  552.   LOGF1("L: COUtilityCar::CImpICar::Steer. Delegating. nAngle=%i.",nAngle);
  553.  
  554.   // Delegate to the contained object implementation of ICar.
  555.   m_pBackObj->m_pICar->Steer(nAngle);
  556.  
  557.   return NOERROR;
  558. }
  559.  
  560.  
  561. /*---------------------------------------------------------------------------
  562.   COUtilityCar's nested implementation of the IUtility interface including
  563.   Constructor, Destructor, QueryInterface, AddRef, Release,
  564.   Offroad, and Winch.
  565. ---------------------------------------------------------------------------*/
  566.  
  567. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  568.   Method:   COUtilityCar::CImpIUtility::CImpIUtility
  569.  
  570.   Summary:  Constructor for the CImpIUtility interface instantiation.
  571.  
  572.   Args:     COUtilityCar* pBackObj,
  573.               Back pointer to the parent outer object.
  574.             IUnknown* pUnkOuter)
  575.               Pointer to the outer Unknown.  For delegation.
  576.  
  577.   Modifies: m_cRefI, m_pBackObj, m_pUnkOuter.
  578.  
  579.   Returns:  void
  580. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  581. COUtilityCar::CImpIUtility::CImpIUtility(
  582.   COUtilityCar* pBackObj,
  583.   IUnknown* pUnkOuter)
  584. {
  585.   // Init the Interface Ref Count (used for debugging only).
  586.   m_cRefI = 0;
  587.  
  588.   // Init the Back Object Pointer to point to the outer object.
  589.   m_pBackObj = pBackObj;
  590.  
  591.   // Init the CImpIUtility interface's delegating Unknown pointer.  We use
  592.   // the Back Object pointer for IUnknown delegation here if we are not
  593.   // being aggregated.  If we are being aggregated we use the supplied
  594.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  595.   // assignment requires no AddRef because the CImpIUtility lifetime is
  596.   // quaranteed by the lifetime of the parent object in which
  597.   // CImpIUtility is nested.
  598.   if (NULL == pUnkOuter)
  599.   {
  600.     m_pUnkOuter = pBackObj;
  601.     LOG("L: COUtilityCar::CImpIUtility Constructor. Non-Aggregating.");
  602.   }
  603.   else
  604.   {
  605.     m_pUnkOuter = pUnkOuter;
  606.     LOG("L: COUtilityCar::CImpIUtility Constructor. Aggregating.");
  607.   }
  608.  
  609.   return;
  610. }
  611.  
  612.  
  613. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  614.   Method:   COUtilityCar::CImpIUtility::~CImpIUtility
  615.  
  616.   Summary:  Destructor for the CImpIUtility interface instantiation.
  617.  
  618.   Args:     void
  619.  
  620.   Modifies: .
  621.  
  622.   Returns:  void
  623. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  624. COUtilityCar::CImpIUtility::~CImpIUtility(void)
  625. {
  626.   LOG("L: COUtilityCar::CImpIUtility Destructor.");
  627.  
  628.   return;
  629. }
  630.  
  631.  
  632. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  633.   Method:   COUtilityCar::CImpIUtility::QueryInterface
  634.  
  635.   Summary:  The QueryInterface IUnknown member of this IUtility interface
  636.             implementation that delegates to m_pUnkOuter, whatever it is.
  637.  
  638.   Args:     REFIID riid,
  639.               [in] GUID of the Interface being requested.
  640.             PPVOID ppv)
  641.               [out] Address of the caller's pointer variable that will
  642.               receive the requested interface pointer.
  643.  
  644.   Modifies: .
  645.  
  646.   Returns:  HRESULT
  647.               Returned by the delegated outer QueryInterface call.
  648. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  649. STDMETHODIMP COUtilityCar::CImpIUtility::QueryInterface(
  650.                REFIID riid,
  651.                PPVOID ppv)
  652. {
  653.   LOG("L: COUtilityCar::CImpIUtility::QueryInterface. Delegating.");
  654.  
  655.   // Delegate this call to the outer object's QueryInterface.
  656.   return m_pUnkOuter->QueryInterface(riid, ppv);
  657. }
  658.  
  659.  
  660. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  661.   Method:   COUtilityCar::CImpIUtility::AddRef
  662.  
  663.   Summary:  The AddRef IUnknown member of this IUtility interface
  664.             implementation that delegates to m_pUnkOuter, whatever it is.
  665.  
  666.   Args:     void
  667.  
  668.   Modifies: m_cRefI.
  669.  
  670.   Returns:  ULONG
  671.               Returned by the delegated outer AddRef call.
  672. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  673. STDMETHODIMP_(ULONG) COUtilityCar::CImpIUtility::AddRef(void)
  674. {
  675.   // Increment the Interface Reference Count.
  676.   ++m_cRefI;
  677.  
  678.   LOGF1("L: COUtilityCar::CImpIUtility::Addref. Delegating. New cI=%i.", m_cRefI);
  679.  
  680.   // Delegate this call to the outer object's AddRef.
  681.   return m_pUnkOuter->AddRef();
  682. }
  683.  
  684.  
  685. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  686.   Method:   COUtilityCar::CImpIUtility::Release
  687.  
  688.   Summary:  The Release IUnknown member of this IUtility interface
  689.             implementation that delegates to m_pUnkOuter, whatever it is.
  690.  
  691.   Args:     void
  692.  
  693.   Modifies: .
  694.  
  695.   Returns:  ULONG
  696.               Returned by the delegated outer Release call.
  697. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  698. STDMETHODIMP_(ULONG) COUtilityCar::CImpIUtility::Release(void)
  699. {
  700.   // Decrement the Interface Reference Count.
  701.   --m_cRefI;
  702.  
  703.   LOGF1("L: COUtilityCar::CImpIUtility::Release. Delegating. New cI=%i.", m_cRefI);
  704.  
  705.   // Delegate this call to the outer object's Release.
  706.   return m_pUnkOuter->Release();
  707. }
  708.  
  709.  
  710. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  711.   Method:   COUtilityCar::CImpIUtility::Offroad
  712.  
  713.   Summary:  The Offroad member method of this IUtility interface
  714.             implementation.  A simple empty method on a COUtilityCar
  715.             COM object for tutorial purposes.  Presumably if this
  716.             UtilityCar object were modeling a real Car then the Offroad
  717.             method would function the 4-wheel drive transfer case and
  718.             shift it to the specified 4-wheel drive mode.
  719.  
  720.   Args:     short nGear
  721.               0 = 2H or regular 2-wheel drive;
  722.               1 = 4H or 4-wheel drive high speed;
  723.               2 = neutral; and
  724.               3 = 4L or 4-wheel drive low speed).
  725.  
  726.   Modifies: .
  727.  
  728.   Returns:  HRESULT
  729.               NOERROR
  730. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  731. STDMETHODIMP COUtilityCar::CImpIUtility::Offroad(
  732.                short nGear)
  733. {
  734.   LOGF1("L: COUtilityCar::CImpIUtility::Offroad. Called. nGear=%i.",nGear);
  735.  
  736.   return NOERROR;
  737. }
  738.  
  739.  
  740. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  741.   Method:   COUtilityCar::CImpIUtility::Winch
  742.  
  743.   Summary:  The Winch member method of this IUtility interface
  744.             implementation.  A simple empty method on a COUtilityCar COM
  745.             object for tutorial purposes.  Presumably if this UtilityCar
  746.             object were modeling a real Car then the Winch method would
  747.             turn on/off the front-mounted Winch to the specified RPMs.
  748.  
  749.   Args:     short nRpm
  750.               0 = off; 1 - 50 RPM.
  751.  
  752.   Modifies: .
  753.  
  754.   Returns:  HRESULT
  755.               NOERROR
  756. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  757. STDMETHODIMP COUtilityCar::CImpIUtility::Winch(
  758.                short nRpm)
  759. {
  760.   LOGF1("L: COUtilityCar::CImpIUtility::Winch. Called. nRpm=%i.",nRpm);
  761.  
  762.   return NOERROR;
  763. }
  764.