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 / dllserve / crucar.cpp < prev    next >
C/C++ Source or Header  |  1997-08-05  |  17KB  |  503 lines

  1. /*+==========================================================================
  2.   File:      CRUCAR.CPP
  3.  
  4.   Summary:   Implementation file for the aggregatable COCruiseCar COM
  5.              object class.
  6.  
  7.              CRUCAR showcases the construction of the COCruiseCar 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.
  11.  
  12.              For a comprehensive tutorial code tour of this module's
  13.              contents and offerings see the tutorial DLLSERVE.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:   COCruiseCar.
  18.  
  19.   Functions: none.
  20.  
  21.   Origin:    9-11-95: atrent - Editor-inheritance from CRUCAR.CPP in
  22.                the COMOBJ 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 ICARS.H and CARGUIDS.H for the common car-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 CAR.H because it has the class COCar declarations.
  50.   We include CRUCAR.H because it has the class COCruiseCar 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 "car.h"
  59. #include "crucar.h"
  60.  
  61.  
  62. /*---------------------------------------------------------------------------
  63.   COCruiseCar'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:   COCruiseCar::COCruiseCar
  69.  
  70.   Summary:  COCruiseCar Constructor. Note the member initializer:
  71.             "m_ImpICruise(this, pUnkOuter)" which is used to pass the
  72.             'this' and pUnkOuter pointers of this constructor function
  73.             to the constructor in the instantiation of the implementation
  74.             of the CImpICruise interface (which is nested inside this
  75.             present COCruiseCar Object Class).
  76.  
  77.   Args:     IUnknown* pUnkOuter,
  78.               Pointer to the the outer Unknown.  NULL means this COM Object
  79.               is not being Aggregated.  Non NULL means it is being created
  80.               on behalf of an outside COM object that is reusing it via
  81.               aggregation.
  82.             CServer* pServer)
  83.               Pointer to the server's control object.
  84.  
  85.   Modifies: m_cRefs, m_pUnkOuter.
  86.  
  87.   Returns:  void
  88. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  89. COCruiseCar::COCruiseCar(
  90.   IUnknown* pUnkOuter,
  91.   CServer* pServer) :
  92.   m_ImpICruise(this, pUnkOuter)
  93. {
  94.   // Zero the COM object's reference count.
  95.   m_cRefs = 0;
  96.  
  97.   // No AddRef necessary if non-NULL, as this present COM object's lifetime
  98.   // is totally coupled with the controlling Outer object's lifetime.
  99.   m_pUnkOuter = pUnkOuter;
  100.  
  101.   // Zero the pointer to the aggregated COCar object's IUnknown
  102.   // interface (for delegation of IUnknown calls to it).
  103.   m_pUnkCar = NULL;
  104.  
  105.   // Assign the pointer to the server's control object.
  106.   m_pServer = pServer;
  107.  
  108.   LOGF1("S: COCruiseCar 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:   COCruiseCar::~COCruiseCar
  116.  
  117.   Summary:  COCruiseCar 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. COCruiseCar::~COCruiseCar(void)
  126. {
  127.   LOG("S: COCruiseCar::Destructor.");
  128.  
  129.   RELEASE_INTERFACE(m_pUnkCar);
  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:   COCruiseCar::Init
  137.  
  138.   Summary:  COCruiseCar Initialization method.
  139.  
  140.   Args:     void
  141.  
  142.   Modifies: m_pUnkCar, m_pICar, m_cRefs.
  143.  
  144.   Returns:  HRESULT
  145.               Standard result code. NOERROR for success.
  146. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  147. HRESULT COCruiseCar::Init(void)
  148. {
  149.   HRESULT hr;
  150.  
  151.   LOG("S: COCruiseCar::Init.");
  152.  
  153.   // Set up the right pIUnknown for delegation.  If we are being
  154.   // aggregated then we pass the pUnkOuter in turn to any COM objects
  155.   // that we are aggregating.  m_pUnkOuter was set in the Constructor.
  156.   IUnknown* pUnkOuter = (NULL == m_pUnkOuter) ? this : m_pUnkOuter;
  157.  
  158.   // We create an instance of the COCar object and do this via the
  159.   // Aggregation reuse technique.  Note we pass pUnkOuter as the
  160.   // Aggregation pointer.  It is the 'this' pointer to this present
  161.   // CruiseCar object if we are not being aggregated; otherwise it is
  162.   // the pointer to the outermost object's controlling IUnknown.  Following
  163.   // the rules of Aggregation we must ask for an IID_IUnknown interface.
  164.   // We cache the requested  pointer to the IUnknown of the new COCar COM
  165.   // object for later use in delegating IUnknown calls.
  166.   hr = CoCreateInstance(
  167.          CLSID_DllCar,
  168.          pUnkOuter,
  169.          CLSCTX_INPROC_SERVER,
  170.          IID_IUnknown,
  171.          (PPVOID)&m_pUnkCar);
  172.  
  173.   if (SUCCEEDED(hr))
  174.   {
  175.     LOG("S: COCruiseCar::Init (New Aggregation of COCar) Succeeded.");
  176.   }
  177.   else
  178.   {
  179.     LOG("S: COCruiseCar::Init (New Aggregation of COCar) Failed.");
  180.   }
  181.  
  182.   return (hr);
  183. }
  184.  
  185.  
  186. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  187.   Method:   COCruiseCar::QueryInterface
  188.  
  189.   Summary:  QueryInterface of the COCruiseCar non-delegating
  190.             IUnknown implementation.
  191.  
  192.   Args:     REFIID riid,
  193.               [in] GUID of the Interface being requested.
  194.             PPVOID ppv)
  195.               [out] Address of the caller's pointer variable that will
  196.               receive the requested interface pointer.
  197.  
  198.   Modifies: .
  199.  
  200.   Returns:  HRESULT
  201. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  202. STDMETHODIMP COCruiseCar::QueryInterface(
  203.                REFIID riid,
  204.                PPVOID ppv)
  205. {
  206.   HRESULT hr = E_NOINTERFACE;
  207.   *ppv = NULL;
  208.  
  209.   if (IID_IUnknown == riid)
  210.   {
  211.     *ppv = this;
  212.     LOG("S: COCruiseCar::QueryInterface. 'this' pIUnknown returned.");
  213.   }
  214.   else if (IID_ICruise == riid)
  215.   {
  216.     // This ICruise interface is implemented in this COCruiseCar object and
  217.     // might be called a native interface of COCruiseCar.
  218.     *ppv = &m_ImpICruise;
  219.     LOG("S: COCruiseCar::QueryInterface. pICruise returned.");
  220.   }
  221.  
  222.   if (NULL != *ppv)
  223.   {
  224.     // We've handed out a pointer to an interface so obey the COM rules
  225.     //   and AddRef its reference count.
  226.     ((LPUNKNOWN)*ppv)->AddRef();
  227.     hr = NOERROR;
  228.   }
  229.   else if (IID_ICar == riid)
  230.   {
  231.     LOG("S: COCruiseCar::QueryInterface. ICar delegating.");
  232.     // We didn't implement the ICar interface in this COCruiseCar object.
  233.     // The aggregated inner COCar object is contributing the ICar
  234.     // interface to this present composite or aggregating CruiseCar object.
  235.     // So, to satisfy a QI request for the ICar interface, we delegate
  236.     // the QueryInterface to the inner object's principal IUnknown.
  237.     hr = m_pUnkCar->QueryInterface(riid, ppv);
  238.   }
  239.  
  240.   return (hr);
  241. }
  242.  
  243.  
  244. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  245.   Method:   COCruiseCar::AddRef
  246.  
  247.   Summary:  AddRef of the COCruiseCar non-delegating IUnknown 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) COCruiseCar::AddRef(void)
  257. {
  258.   m_cRefs++;
  259.  
  260.   LOGF1("S: COCruiseCar::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:   COCruiseCar::Release
  268.  
  269.   Summary:  Release of the COCruiseCar non-delegating IUnknown implementation.
  270.  
  271.   Args:     void
  272.  
  273.   Modifies: m_cRefs.
  274.  
  275.   Returns:  ULONG
  276.               New value of m_cRefs (COM object's reference count).
  277. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  278. STDMETHODIMP_(ULONG) COCruiseCar::Release(void)
  279. {
  280.   ULONG ulCount = --m_cRefs;
  281.  
  282.   LOGF1("S: COCruiseCar::Release. New cRefs=%i.", m_cRefs);
  283.  
  284.   if (0 == m_cRefs)
  285.   {
  286.     // We've reached a zero reference count for this COM object.
  287.     // So we tell the server housing to decrement its global object
  288.     // count so that the server will be unloaded if appropriate.
  289.     if (NULL != m_pServer)
  290.       m_pServer->ObjectsDown();
  291.  
  292.     // We artificially bump the main ref count.  This fulfills one of
  293.     // the rules of aggregated objects and ensures that an indirect
  294.     // recursive call to this release won't occur because of other
  295.     // delegating releases that might happen in our own destructor.
  296.     m_cRefs++;
  297.     delete this;
  298.   }
  299.  
  300.   return ulCount;
  301. }
  302.  
  303.  
  304. /*---------------------------------------------------------------------------
  305.   COCruiseCar's nested implementation of the ICruise interface including
  306.   Constructor, Destructor, QueryInterface, AddRef, Release,
  307.   Engage, and Adjust.
  308. ---------------------------------------------------------------------------*/
  309.  
  310. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  311.   Method:   COCruiseCar::CImpICruise::CImpICruise
  312.  
  313.   Summary:  Constructor for the CImpICruise interface instantiation.
  314.  
  315.   Args:     COCruiseCar* pBackObj,
  316.               Back pointer to the parent outer object.
  317.             IUnknown* pUnkOuter)
  318.               Pointer to the outer Unknown.  For delegation.
  319.  
  320.   Modifies: m_cRefI, m_pBackObj, m_pUnkOuter.
  321.  
  322.   Returns:  void
  323. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  324. COCruiseCar::CImpICruise::CImpICruise(
  325.   COCruiseCar* pBackObj,
  326.   IUnknown* pUnkOuter)
  327. {
  328.   // Init the Interface Ref Count (used for debugging only).
  329.   m_cRefI = 0;
  330.  
  331.   // Init the Back Object Pointer to point to the outer object.
  332.   m_pBackObj = pBackObj;
  333.  
  334.   // Init the CImpICruise interface's delegating Unknown pointer.  We use
  335.   // the Back Object pointer for IUnknown delegation here if we are not
  336.   // being aggregated.  If we are being aggregated we use the supplied
  337.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  338.   // assignment requires no AddRef because the CImpICruise lifetime is
  339.   // quaranteed by the lifetime of the parent object in which
  340.   // CImpICruise is nested.
  341.   if (NULL == pUnkOuter)
  342.   {
  343.     m_pUnkOuter = pBackObj;
  344.     LOG("S: COCruiseCar::CImpICruise Constructor. Non-Aggregating.");
  345.   }
  346.   else
  347.   {
  348.     m_pUnkOuter = pUnkOuter;
  349.     LOG("S: COCruiseCar::CImpICruise Constructor. Aggregating.");
  350.   }
  351.  
  352.   return;
  353. }
  354.  
  355.  
  356. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  357.   Method:   COCruiseCar::CImpICruise::~CImpICruise
  358.  
  359.   Summary:  Destructor for the CImpICruise interface instantiation.
  360.  
  361.   Args:     void
  362.  
  363.   Modifies: .
  364.  
  365.   Returns:  void
  366. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  367. COCruiseCar::CImpICruise::~CImpICruise(void)
  368. {
  369.   LOG("S: COCruiseCar::CImpICruise Destructor.");
  370.  
  371.   return;
  372. }
  373.  
  374.  
  375. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  376.   Method:   COCruiseCar::CImpICruise::QueryInterface
  377.  
  378.   Summary:  The QueryInterface IUnknown member of this ICruise interface
  379.             implementation that delegates to m_pUnkOuter, whatever it is.
  380.  
  381.   Args:     REFIID riid,
  382.               [in] GUID of the Interface being requested.
  383.             PPVOID ppv)
  384.               [out] Address of the caller's pointer variable that will
  385.               receive the requested interface pointer.
  386.  
  387.   Modifies: .
  388.  
  389.   Returns:  HRESULT
  390.               Returned by the delegated outer QueryInterface call.
  391. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  392. STDMETHODIMP COCruiseCar::CImpICruise::QueryInterface(
  393.                REFIID riid,
  394.                PPVOID ppv)
  395. {
  396.   LOG("S: COCruiseCar::CImpICruise::QueryInterface. Delegating.");
  397.  
  398.   // Delegate this call to the outer object's QueryInterface.
  399.   return m_pUnkOuter->QueryInterface(riid, ppv);
  400. }
  401.  
  402.  
  403. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  404.   Method:   COCruiseCar::CImpICruise::AddRef
  405.  
  406.   Summary:  The AddRef IUnknown member of this ICruise interface
  407.             implementation that delegates to m_pUnkOuter, whatever it is.
  408.  
  409.   Args:     void
  410.  
  411.   Modifies: m_cRefI.
  412.  
  413.   Returns:  ULONG
  414.               Returned by the delegated outer AddRef call.
  415. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  416. STDMETHODIMP_(ULONG) COCruiseCar::CImpICruise::AddRef(void)
  417. {
  418.   // Increment the Interface Reference Count.
  419.   ++m_cRefI;
  420.  
  421.   LOGF1("S: COCruiseCar::CImpICruise::Addref. Delegating. New cI=%i.", m_cRefI);
  422.  
  423.   // Delegate this call to the outer object's AddRef.
  424.   return m_pUnkOuter->AddRef();
  425. }
  426.  
  427.  
  428. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  429.   Method:   COCruiseCar::CImpICruise::Release
  430.  
  431.   Summary:  The Release IUnknown member of this ICruise interface
  432.             implementation that delegates to m_pUnkOuter, whatever it is.
  433.  
  434.   Args:     void
  435.  
  436.   Modifies: .
  437.  
  438.   Returns:  ULONG
  439.               Returned by the delegated outer Release call.
  440. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  441. STDMETHODIMP_(ULONG) COCruiseCar::CImpICruise::Release(void)
  442. {
  443.   // Decrement the Interface Reference Count.
  444.   --m_cRefI;
  445.  
  446.   LOGF1("S: COCruiseCar::CImpICruise::Release. Delegating. New cI=%i.",m_cRefI);
  447.  
  448.   // Delegate this call to the outer object's Release.
  449.   return m_pUnkOuter->Release();
  450. }
  451.  
  452.  
  453. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  454.   Method:   COCruiseCar::CImpICruise::Engage
  455.  
  456.   Summary:  The Engage member method of this ICruise interface
  457.             implementation.  A simple empty method on a COCruiseCar COM
  458.             object for tutorial purposes.  Presumably if this Car object
  459.             were modeling a real Car then the Engage method would turn
  460.             the Cruise control system on or off.
  461.  
  462.   Args:     BOOL bOnOff)
  463.               TRUE for On; FALSE for Off.
  464.  
  465.   Modifies: .
  466.  
  467.   Returns:  HRESULT
  468.               NOERROR
  469. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  470. STDMETHODIMP COCruiseCar::CImpICruise::Engage(
  471.                BOOL bOnOff)
  472. {
  473.   LOGF1("S: COCruiseCar::CImpICruise::Engage. Called. bOnOff=%i.",bOnOff);
  474.  
  475.   return NOERROR;
  476. }
  477.  
  478.  
  479. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  480.   Method:   COCruiseCar::CImpICruise::Adjust
  481.  
  482.   Summary:  The Adjust member method of this ICruise interface
  483.             implementation.  A simple empty method on a COCruiseCar COM
  484.             object for tutorial purposes.  Presumably if this Car object
  485.             were modeling a real Car then the Adjust method would allow
  486.             notching the cruise set speed up or down by increments of 3 mph.
  487.  
  488.   Args:     BOOL bUpDown)
  489.               TRUE for Up; FALSE for Down.
  490.  
  491.   Modifies: .
  492.  
  493.   Returns:  HRESULT
  494.               NOERROR
  495. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  496. STDMETHODIMP COCruiseCar::CImpICruise::Adjust(
  497.                BOOL bUpDown)
  498. {
  499.   LOGF1("S: COCruiseCar::CImpICruise::Adjust. Called. bUpDown=%i.",bUpDown);
  500.  
  501.   return NOERROR;
  502. }
  503.