home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_2.iso / windows / program / activex / axtsamp.exe / TSBRANCH.EXE / COMOBJ / CRUCAR.CPP < prev    next >
C/C++ Source or Header  |  1997-01-12  |  16KB  |  478 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 object
  8.              class with the IUnknown, ICar, and ICruise interfaces.  This
  9.              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 COMOBJ.HTM
  14.              file.  For more specific technical details on the internal
  15.              workings see the comments dispersed throughout the
  16.              module's source code.
  17.  
  18.   Classes:   COCruiseCar
  19.  
  20.   Functions: none.
  21.  
  22.   Origin:    8-23-95: atrent - Created.
  23.  
  24. ----------------------------------------------------------------------------
  25.   This file is part of the Microsoft ActiveX 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 make calls to 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 COMOBJI.H because it has internal class declarations and
  48.     resource ID definitions specific for this DLL.
  49.   We include COMOBJ.H because it has the interface declarations.
  50.   We include CAR.H because it has the class COCar declarations.
  51.   We include CRUCAR.H because it has the class COCruiseCar declarations.
  52. ---------------------------------------------------------------------------*/
  53. #include <windows.h>
  54. #include <ole2.h>
  55. #include <apputil.h>
  56. #include <icars.h>
  57. #include <carguids.h>
  58. #include "comobji.h"
  59. #include "comobj.h"
  60. #include "car.h"
  61. #include "crucar.h"
  62.  
  63.  
  64. /*---------------------------------------------------------------------------
  65.   COCruiseCar's implementation of its main COM object class including
  66.   Constructor, Destructor, QueryInterface, AddRef, and Release.
  67. ---------------------------------------------------------------------------*/
  68.  
  69. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  70.   Method:   COCruiseCar::COCruiseCar
  71.  
  72.   Summary:  COCruiseCar Constructor. Note the member initializer:
  73.             "m_ImpICruise(this, pUnkOuter)" which is used to pass the
  74.             'this' and pUnkOuter pointers of this constructor function
  75.             to the constructor in the instantiation of the implementation
  76.             of the CImpICar interface (which is nested inside this present
  77.             COCruiseCar Object Class).
  78.  
  79.   Args:     IUnknown* pUnkOuter)
  80.               Pointer to the the outer Unknown.  NULL means this COM Object
  81.               is not being Aggregated.  Non NULL means it is being created
  82.               on behalf of an outside COM object that is reusing it via
  83.               aggregation.
  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.   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.   LOGF1("D: COCruiseCar Constructor. m_pUnkOuter=0x%X.", m_pUnkOuter);
  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:   COCruiseCar::~COCruiseCar
  112.  
  113.   Summary:  COCruiseCar Destructor.
  114.  
  115.   Args:     void
  116.  
  117.   Modifies: .
  118.  
  119.   Returns:  void
  120. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  121. COCruiseCar::~COCruiseCar(void)
  122. {
  123.   LOG("D: COCruiseCar::Destructor.");
  124.  
  125.   RELEASE_INTERFACE(m_pUnkCar);
  126.  
  127.   return;
  128. }
  129.  
  130.  
  131. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  132.   Method:   COCruiseCar::Init
  133.  
  134.   Summary:  COCruiseCar Initialization method.
  135.  
  136.   Args:     void
  137.  
  138.   Modifies: m_pUnkCar, m_pICar, m_cRefs.
  139.  
  140.   Returns:  HRESULT
  141.               Standard result code. NOERROR for success.
  142. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  143. HRESULT COCruiseCar::Init(void)
  144. {
  145.   HRESULT hr;
  146.   // Set up the right pIUnknown for delegation.  If we are being
  147.   // aggregated then we pass the pUnkOuter in turn to any COM objects
  148.   // that we are aggregating.  m_pUnkOuter was set in the Constructor.
  149.   IUnknown* pUnkOuter = (NULL == m_pUnkOuter) ? this : m_pUnkOuter;
  150.  
  151.   LOG("D: COCruiseCar::Init.");
  152.  
  153.   // We create an instance of the COCar object and do this via the
  154.   // Aggregation reuse technique.  Note we pass pUnkOuter as the
  155.   // Aggregation pointer.  It is the 'this' pointer to this present
  156.   // CruiseCar object if we are not being aggregated; otherwise it is
  157.   // the pointer to the outermost object's controlling IUnknown.  Following
  158.   // the rules of Aggregation we ask CreateCar for an IID_IUnknown
  159.   // interface.  We cache this pointer to the IUnknown of the new COCar
  160.   // COM object for later use in delegating IUnknown calls.
  161.   hr = CreateCar(pUnkOuter, IID_IUnknown, (PPVOID)&m_pUnkCar);
  162.  
  163.   return (hr);
  164. }
  165.  
  166.  
  167. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  168.   Method:   COCruiseCar::QueryInterface
  169.  
  170.   Summary:  QueryInterface of the COCruiseCar non-delegating
  171.             IUnknown implementation.
  172.  
  173.   Args:     REFIID riid,
  174.               [in] GUID of the Interface being requested.
  175.             PPVOID ppv)
  176.               [out] Address of the caller's pointer variable that will
  177.               receive the requested interface pointer.
  178.  
  179.   Modifies: .
  180.  
  181.   Returns:  HRESULT
  182. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  183. STDMETHODIMP COCruiseCar::QueryInterface(
  184.                REFIID riid,
  185.                PPVOID ppv)
  186. {
  187.   HRESULT hr = E_NOINTERFACE;
  188.   *ppv = NULL;
  189.  
  190.   if (IID_IUnknown == riid)
  191.   {
  192.     *ppv = this;
  193.     LOG("D: COCruiseCar::QueryInterface. 'this' pIUnknown returned.");
  194.   }
  195.   else if (IID_ICruise == riid)
  196.   {
  197.     // This ICruise interface is implemented in this COCruiseCar object and
  198.     // might be called a native interface of COCruiseCar.
  199.     *ppv = &m_ImpICruise;
  200.     LOG("D: COCruiseCar::QueryInterface. pICruise returned.");
  201.   }
  202.  
  203.   if (NULL != *ppv)
  204.   {
  205.     // We've handed out a pointer to an interface so obey the COM rules
  206.     //   and AddRef its reference count.
  207.     ((LPUNKNOWN)*ppv)->AddRef();
  208.     hr = NOERROR;
  209.   }
  210.   else if (IID_ICar == riid)
  211.   {
  212.     LOG("D: COCruiseCar::QueryInterface. ICar delegating.");
  213.     // We didn't implement the ICar interface in this COCruiseCar object.
  214.     // The aggregated inner COCar object is contributing the ICar
  215.     // interface to this present composite or aggregating CruiseCar object.
  216.     // So, to satisfy a QI request for the ICar interface, we delegate
  217.     // the QueryInterface to the inner object's controlling IUnknown.
  218.     hr = m_pUnkCar->QueryInterface(riid, ppv);
  219.   }
  220.  
  221.   return (hr);
  222. }
  223.  
  224.  
  225. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  226.   Method:   COCruiseCar::AddRef
  227.  
  228.   Summary:  AddRef of the COCruiseCar non-delegating IUnknown implementation.
  229.  
  230.   Args:     void
  231.  
  232.   Modifies: m_cRefs.
  233.  
  234.   Returns:  ULONG
  235.               New value of m_cRefs (COM object's reference count).
  236. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  237. STDMETHODIMP_(ULONG) COCruiseCar::AddRef(void)
  238. {
  239.   m_cRefs++;
  240.  
  241.   LOGF1("D: COCruiseCar::AddRef. New cRefs=%i.", m_cRefs);
  242.  
  243.   return m_cRefs;
  244. }
  245.  
  246.  
  247. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  248.   Method:   COCruiseCar::Release
  249.  
  250.   Summary:  Release of the COCruiseCar non-delegating IUnknown implementation.
  251.  
  252.   Args:     void
  253.  
  254.   Modifies: m_cRefs.
  255.  
  256.   Returns:  ULONG
  257.               New value of m_cRefs (COM object's reference count).
  258. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  259. STDMETHODIMP_(ULONG) COCruiseCar::Release(void)
  260. {
  261.   ULONG ulCount = --m_cRefs;
  262.  
  263.   LOGF1("D: COCruiseCar::Release. New cRefs=%i.", m_cRefs);
  264.  
  265.   if (0 == m_cRefs)
  266.   {
  267.     // We artificially bump the main ref count.  This fulfills one of
  268.     // the rules of aggregated objects and ensures that an indirect
  269.     // recursive call to this release won't occur because of other
  270.     // delegating releases that might happen in our own destructor.
  271.     m_cRefs++;
  272.     delete this;
  273.   }
  274.  
  275.   return ulCount;
  276. }
  277.  
  278.  
  279. /*---------------------------------------------------------------------------
  280.   COCruiseCar's nested implementation of the ICruise interface including
  281.   Constructor, Destructor, QueryInterface, AddRef, Release,
  282.   Engage, and Adjust.
  283. ---------------------------------------------------------------------------*/
  284.  
  285. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  286.   Method:   COCruiseCar::CImpICruise::CImpICruise
  287.  
  288.   Summary:  Constructor for the CImpICruise interface instantiation.
  289.  
  290.   Args:     COCruiseCar* pBackObj,
  291.               Back pointer to the parent outer object.
  292.             IUnknown* pUnkOuter)
  293.               Pointer to the outer Unknown.  For delegation.
  294.  
  295.   Modifies: m_cRefI, m_pBackObj, m_pUnkOuter.
  296.  
  297.   Returns:  void
  298. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  299. COCruiseCar::CImpICruise::CImpICruise(
  300.   COCruiseCar* pBackObj,
  301.   IUnknown* pUnkOuter)
  302. {
  303.   // Init the Interface Ref Count (used for debugging only).
  304.   m_cRefI = 0;
  305.  
  306.   // Init the Back Object Pointer to point to the outer object.
  307.   m_pBackObj = pBackObj;
  308.  
  309.   // Init the CImpICruise interface's delegating Unknown pointer.  We use
  310.   // the Back Object pointer for IUnknown delegation here if we are not
  311.   // being aggregated.  If we are being aggregated we use the supplied
  312.   // pUnkOuter for IUnknown delegation.  In either case the pointer
  313.   // assignment requires no AddRef because the CImpICruise lifetime is
  314.   // quaranteed by the lifetime of the parent object in which
  315.   // CImpICruise is nested.
  316.   if (NULL == pUnkOuter)
  317.   {
  318.     m_pUnkOuter = pBackObj;
  319.     LOG("D: COCruiseCar::CImpICruise Constructor. Non-Aggregating.");
  320.   }
  321.   else
  322.   {
  323.     m_pUnkOuter = pUnkOuter;
  324.     LOG("D: COCruiseCar::CImpICruise Constructor. Aggregating.");
  325.   }
  326.  
  327.   return;
  328. }
  329.  
  330.  
  331. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  332.   Method:   COCruiseCar::CImpICruise::~CImpICruise
  333.  
  334.   Summary:  Destructor for the CImpICruise interface instantiation.
  335.  
  336.   Args:     void
  337.  
  338.   Modifies: .
  339.  
  340.   Returns:  void
  341. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  342. COCruiseCar::CImpICruise::~CImpICruise(void)
  343. {
  344.   LOG("D: COCruiseCar::CImpICruise Destructor.");
  345.  
  346.   return;
  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:   COCruiseCar::CImpICruise::QueryInterface
  352.  
  353.   Summary:  The QueryInterface IUnknown member of this ICruise interface
  354.             implementation that delegates to m_pUnkOuter, whatever it is.
  355.  
  356.   Args:     REFIID riid,
  357.               [in] GUID of the Interface being requested.
  358.             PPVOID ppv)
  359.               [out] Address of the caller's pointer variable that will
  360.               receive the requested interface pointer.
  361.  
  362.   Modifies: .
  363.  
  364.   Returns:  HRESULT
  365.               Returned by the delegated outer QueryInterface call.
  366. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  367. STDMETHODIMP COCruiseCar::CImpICruise::QueryInterface(
  368.                REFIID riid,
  369.                PPVOID ppv)
  370. {
  371.   LOG("D: COCruiseCar::CImpICruise::QueryInterface. Delegating.");
  372.  
  373.   // Delegate this call to the outer object's QueryInterface.
  374.   return m_pUnkOuter->QueryInterface(riid, ppv);
  375. }
  376.  
  377.  
  378. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  379.   Method:   COCruiseCar::CImpICruise::AddRef
  380.  
  381.   Summary:  The AddRef IUnknown member of this ICruise interface
  382.             implementation that delegates to m_pUnkOuter, whatever it is.
  383.  
  384.   Args:     void
  385.  
  386.   Modifies: m_cRefI.
  387.  
  388.   Returns:  ULONG
  389.               Returned by the delegated outer AddRef call.
  390. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  391. STDMETHODIMP_(ULONG) COCruiseCar::CImpICruise::AddRef(void)
  392. {
  393.   // Increment the Interface Reference Count.
  394.   ++m_cRefI;
  395.  
  396.   LOGF1("D: COCruiseCar::CImpICruise::Addref. Delegating. New cI=%i.", m_cRefI);
  397.  
  398.   // Delegate this call to the outer object's AddRef.
  399.   return m_pUnkOuter->AddRef();
  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::Release
  405.  
  406.   Summary:  The Release IUnknown member of this ICruise interface
  407.             implementation that delegates to m_pUnkOuter, whatever it is.
  408.  
  409.   Args:     void
  410.  
  411.   Modifies: .
  412.  
  413.   Returns:  ULONG
  414.               Returned by the delegated outer Release 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::Release(void)
  417. {
  418.   // Decrement the Interface Reference Count.
  419.   --m_cRefI;
  420.  
  421.   LOGF1("D: COCruiseCar::CImpICruise::Release. Delegating. New cI=%i.", m_cRefI);
  422.  
  423.   // Delegate this call to the outer object's Release.
  424.   return m_pUnkOuter->Release();
  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::Engage
  430.  
  431.   Summary:  The Engage member method of this ICruise interface
  432.             implementation.  A simple empty method on a COCruiseCar COM
  433.             object for tutorial purposes.  Presumably if this Car object
  434.             were modeling a real Car then the Engage method would turn
  435.             the Cruise control system on or off.
  436.  
  437.   Args:     BOOL bOnOff)
  438.               TRUE for On; FALSE for Off.
  439.  
  440.   Modifies: .
  441.  
  442.   Returns:  HRESULT
  443.               NOERROR
  444. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  445. STDMETHODIMP COCruiseCar::CImpICruise::Engage(
  446.                BOOL bOnOff)
  447. {
  448.   LOGF1("D: COCruiseCar::CImpICruise::Engage. Called. bOnOff=%i.",bOnOff);
  449.  
  450.   return NOERROR;
  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:   COCruiseCar::CImpICruise::Adjust
  456.  
  457.   Summary:  The Adjust member method of this ICruise interface
  458.             implementation.  A simple empty method on a COCruiseCar COM
  459.             object for tutorial purposes.  Presumably if this Car object
  460.             were modeling a real Car then the Adjust method would allow
  461.             notching the cruise set speed up or down by increments of 3 mph.
  462.  
  463.   Args:     BOOL bUpDown)
  464.               TRUE for Up; FALSE for Down.
  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 COCruiseCar::CImpICruise::Adjust(
  472.                BOOL bUpDown)
  473. {
  474.   LOGF1("D: COCruiseCar::CImpICruise::Adjust. Called. bUpDown=%i.",bUpDown);
  475.  
  476.   return NOERROR;
  477. }
  478.