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 / dcdserve / connect.cpp next >
Encoding:
C/C++ Source or Header  |  1997-08-30  |  44.4 KB  |  1,464 lines

  1. /*+==========================================================================
  2.   File:      CONNECT.CPP
  3.  
  4.   Summary:   Implementation file for the connection points (and their
  5.              connections) offered by the connectable objects in the
  6.              DCDSERVE server sample. COM objects are implemented for
  7.              Connection Point Enumerators, Connection Points, and
  8.              Connection Enumerators.
  9.  
  10.              For a comprehensive tutorial code tour of this module's
  11.              contents and offerings see the tutorial DCDSERVE.HTM
  12.              file. For more specific technical details on the internal
  13.              workings see the comments dispersed throughout the module's
  14.              source code.
  15.  
  16.   Classes:   COEnumConnectionPoints, COConnectionPoint, and
  17.              COEnumConnections.
  18.  
  19.   Functions: none.
  20.  
  21.   Origin:    6-10-96: atrent - Editor inheritance from CONSERVE COM
  22.              Tutorial Code Sample. Very little change was required.
  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. /*---------------------------------------------------------------------------
  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 OLECTL.H because it has definitions for connectable objects.
  44.   We include APPUTIL.H because we will be building this application using
  45.     the convenient Virtual Window and Dialog classes and other
  46.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  47.   We include PAPINT.H and PAPGUIDS.H for the common paper-related
  48.     Interface class, GUID, and CLSID specifications.
  49.   We include SERVER.H because it has internal class declarations and
  50.     resource ID definitions specific for this server.
  51.   We include CONNECT.H for object class declarations for the various
  52.     connection point and connection COM objects used in CONSERVE.
  53.   We include PAPER.H because it has the class COEnumConnectionPoints
  54.     declarations as well as the COPaper class declaration.
  55. ---------------------------------------------------------------------------*/
  56. #include <windows.h>
  57. #include <ole2.h>
  58. #include <olectl.h>
  59. #include <apputil.h>
  60. #include <papint.h>
  61. #include <papguids.h>
  62. #include "server.h"
  63. #include "connect.h"
  64. #include "paper.h"
  65.  
  66.  
  67. /*---------------------------------------------------------------------------
  68.   COEnumConnectionPoints's implementation of its main COM object class
  69.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  70.   Next, Skip, Reset, and Clone.
  71. ---------------------------------------------------------------------------*/
  72.  
  73. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  74.   Method:   COEnumConnectionPoints::COEnumConnectionPoints
  75.  
  76.   Summary:  COEnumConnectionPoints Constructor.
  77.  
  78.   Args:     IUnknown* pHostobj
  79.               Pointer to the host object whose connection points are
  80.               being enumerated.
  81.  
  82.   Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnPts, and m_paConnPts.
  83.  
  84.   Returns:  void
  85. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  86. COEnumConnectionPoints::COEnumConnectionPoints(
  87.   IUnknown* pHostObj)
  88. {
  89.   // Zero the COM object's reference count.
  90.   m_cRefs = 0;
  91.  
  92.   // Assign the Host Object pointer.
  93.   m_pHostObj = pHostObj;
  94.  
  95.   // Initialize the Connection Point enumerator variables.
  96.   m_iEnumIndex = 0;
  97.   m_cConnPts = 0;
  98.   m_paConnPts = NULL;
  99.  
  100.   return;
  101. }
  102.  
  103.  
  104. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  105.   Method:   COEnumConnectionPoints::~COEnumConnectionPoints
  106.  
  107.   Summary:  COEnumConnectionPoints Destructor.
  108.  
  109.   Args:     void
  110.  
  111.   Modifies: m_paConnPts.
  112.  
  113.   Returns:  void
  114. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  115. COEnumConnectionPoints::~COEnumConnectionPoints(void)
  116. {
  117.   if (NULL != m_paConnPts)
  118.   {
  119.     UINT i;
  120.  
  121.     // Release all the connection point interface pointers.
  122.     for (i=0; i<m_cConnPts; i++)
  123.       if (NULL != m_paConnPts[i])
  124.         m_paConnPts[i]->Release();
  125.  
  126.     // Delete the array of interface pointers.
  127.     delete [] m_paConnPts;
  128.   }
  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:   COEnumConnectionPoints::Init
  136.  
  137.   Summary:  COEnumConnectionPoints Initialization method.  Create any
  138.             necessary arrays, structures, and objects.
  139.  
  140.   Args:     ULONG cConnPts,
  141.               Number of Connections Points.
  142.             IConnectionPoint** paConnPts,
  143.               Pointer to array of connection point interface pointers.
  144.             ULONG iEnumIndex
  145.               The initial Enumerator index value.
  146.  
  147.   Modifies: m_cConnPts, m_paConnPts, m_iEnumIndex.
  148.  
  149.   Returns:  HRESULT
  150.               Standard result code. NOERROR for success.
  151. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  152. HRESULT COEnumConnectionPoints::Init(
  153.           ULONG cConnPts,
  154.           IConnectionPoint** paConnPts,
  155.           ULONG iEnumIndex)
  156. {
  157.   HRESULT hr = NOERROR;
  158.   UINT i;
  159.  
  160.   // Remember the number of Connection points.
  161.   m_cConnPts = cConnPts;
  162.  
  163.   // Remember the initial enumerator index.
  164.   m_iEnumIndex = iEnumIndex;
  165.  
  166.   // Create a copy of the array of connection points and keep it inside
  167.   // this enumerator COM object.
  168.   m_paConnPts = new IConnectionPoint* [(UINT) cConnPts];
  169.  
  170.   // Fill the array copy with the IConnectionPoint interface pointers from
  171.   // the array passed. AddRef for each new Interface pointer copy made.
  172.   if (NULL != m_paConnPts)
  173.   {
  174.     for (i=0; i<cConnPts; i++)
  175.     {
  176.       m_paConnPts[i] = paConnPts[i];
  177.       m_paConnPts[i]->AddRef();
  178.     }
  179.   }
  180.   else
  181.     hr = E_OUTOFMEMORY;
  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:   COEnumConnectionPoints::QueryInterface
  189.  
  190.   Summary:  QueryInterface of the COEnumConnectionPoints 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.   Returns:  HRESULT
  200.               Standard result code. NOERROR for success.
  201. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  202. STDMETHODIMP COEnumConnectionPoints::QueryInterface(
  203.                REFIID riid,
  204.                PPVOID ppv)
  205. {
  206.   HRESULT hr = E_NOINTERFACE;
  207.  
  208.   *ppv = NULL;
  209.  
  210.   // The IEnumConnectionPoints interface is implemented directly in
  211.   // this COM object rather than being a nested interface implementation.
  212.   if (IID_IUnknown == riid || IID_IEnumConnectionPoints == riid)
  213.     *ppv = (LPVOID)this;
  214.  
  215.   if (NULL != *ppv)
  216.   {
  217.     // We've handed out a pointer to the interface so obey the COM rules
  218.     // and AddRef the reference count.
  219.     ((LPUNKNOWN)*ppv)->AddRef();
  220.     hr = NOERROR;
  221.   }
  222.  
  223.   return (hr);
  224. }
  225.  
  226.  
  227. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  228.   Method:   COEnumConnectionPoints::AddRef
  229.  
  230.   Summary:  AddRef of the COEnumConnectionPoints non-delegating IUnknown
  231.             implementation.
  232.  
  233.   Args:     void
  234.  
  235.   Modifies: m_cRefs.
  236.  
  237.   Returns:  ULONG
  238.               New value of m_cRefs (COM object's reference count).
  239. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  240. STDMETHODIMP_(ULONG) COEnumConnectionPoints::AddRef(void)
  241. {
  242.   ULONG cRefs;
  243.  
  244.   cRefs = ++m_cRefs;
  245.  
  246.   // Also AddRef the host object to ensure it stays around as long as
  247.   // this enumerator.
  248.   m_pHostObj->AddRef();
  249.  
  250.   return cRefs;
  251. }
  252.  
  253.  
  254. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  255.   Method:   COEnumConnectionPoints::Release
  256.  
  257.   Summary:  Release of the COEnumConnectionPoints non-delegating IUnknown
  258.             implementation.
  259.  
  260.   Args:     void
  261.  
  262.   Modifies: m_cRefs.
  263.  
  264.   Returns:  ULONG
  265.               New value of m_cRefs (COM object's reference count).
  266. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  267. STDMETHODIMP_(ULONG) COEnumConnectionPoints::Release(void)
  268. {
  269.   ULONG cRefs;
  270.  
  271.   // Pass this release along to the Host object being enumerated.
  272.   m_pHostObj->Release();
  273.  
  274.   cRefs = --m_cRefs;
  275.  
  276.   if (0 == cRefs)
  277.   {
  278.     // We artificially bump the main ref count to prevent reentrancy via
  279.     // the main object destructor.
  280.     m_cRefs++;
  281.     delete this;
  282.   }
  283.  
  284.   return cRefs;
  285. }
  286.  
  287.  
  288. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  289.   Method:   COEnumConnectionPoints::Next
  290.  
  291.   Summary:  The Next member method of this IEnumConnectionPoints interface
  292.             implementation. Called by outside clients of a
  293.             COEnumConnectionPoints object to request that a number of next
  294.             connection point interface pointers be deposited into an array
  295.             supplied by the caller.
  296.  
  297.   Args:     ULONG cReq
  298.               Number of connection points requested for return (starting at
  299.               the current Enumerator index).
  300.             IConnectionPoint** paConnPts,
  301.               Pointer to a caller's output array that will receive the
  302.               enumerated IConnectionPoint interface pointers.
  303.             ULONG* cEnumerated)
  304.               Pointer to a ULONG variable that will contain the number of
  305.               connection points actually enumerated by this call.
  306.  
  307.   Returns:  HRESULT
  308.               Standard result code. NOERROR for success.
  309. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  310. STDMETHODIMP COEnumConnectionPoints::Next(
  311.                ULONG cReq,
  312.                IConnectionPoint** paConnPts,
  313.                ULONG* pcEnumerated)
  314. {
  315.   HRESULT hr = NOERROR;
  316.   ULONG cRet = 0;
  317.  
  318.   // Make sure the argument values passed are valid.
  319.   if (NULL != m_paConnPts)
  320.   {
  321.     if (NULL != paConnPts)
  322.     {
  323.       if (NULL != *paConnPts && m_iEnumIndex < m_cConnPts)
  324.       {
  325.         if (NULL != pcEnumerated)
  326.           *pcEnumerated = 0L;
  327.         else
  328.           if (1L != cReq)
  329.             hr = E_POINTER;
  330.       }
  331.       else
  332.         hr = S_FALSE;
  333.     }
  334.     else
  335.       hr = E_POINTER;
  336.   }
  337.   else
  338.     hr = S_FALSE;
  339.  
  340.   if (SUCCEEDED(hr))
  341.   {
  342.     // Starting at the current Enumerator index, loop to assign the
  343.     // requested number of output connection point interface pointers.
  344.     for (; m_iEnumIndex < m_cConnPts && cReq > 0;
  345.            paConnPts++, cRet++, cReq--)
  346.     {
  347.       // Assign from the inside Enumerator array to the specified receiving
  348.       // array.
  349.       *paConnPts = m_paConnPts[m_iEnumIndex++];
  350.       // After assigning a copy of an IConnectionPoint pointer, AddRef it.
  351.       if (NULL != *paConnPts)
  352.         (*paConnPts)->AddRef();
  353.     }
  354.  
  355.     // Assign the output number of connection points enumerated.
  356.     if (NULL != pcEnumerated)
  357.       *pcEnumerated = cRet;
  358.   }
  359.  
  360.   return hr;
  361. }
  362.  
  363.  
  364. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  365.   Method:   COEnumConnectionPoints::Skip
  366.  
  367.   Summary:  The Skip member method of this IEnumConnectionPoints interface
  368.             implementation. Starting at the current Enumerator index, skip
  369.             the specified number of connection point items.
  370.  
  371.   Args:     ULONG cSkip
  372.               Number of Connection Point items to skip.
  373.  
  374.   Returns:  HRESULT
  375.               Standard result code. NOERROR for success.
  376. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  377. STDMETHODIMP COEnumConnectionPoints::Skip(
  378.                ULONG cSkip)
  379. {
  380.   HRESULT hr = NOERROR;
  381.  
  382.   // If there really is a connection point array and the requested
  383.   // amount of skip does not exceed the number of connection points,
  384.   // then bump the index by the requested skip amount.
  385.   if (NULL != m_paConnPts && (m_iEnumIndex + cSkip) < m_cConnPts)
  386.     m_iEnumIndex += cSkip;
  387.   else
  388.     hr = S_FALSE;
  389.  
  390.   return hr;
  391. }
  392.  
  393.  
  394. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  395.   Method:   COEnumConnectionPoints::Reset
  396.  
  397.   Summary:  The Reset member method of the IEnumConnectionPoints interface
  398.             implementation. Resets the Enumeration index to the first
  399.             connection point item in the array.
  400.  
  401.   Args:     void.
  402.  
  403.   Returns:  HRESULT
  404.               Standard result code. NOERROR for success.
  405. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  406. STDMETHODIMP COEnumConnectionPoints::Reset(
  407.                void)
  408. {
  409.   // Zero the main Enumerator index.
  410.   m_iEnumIndex = 0;
  411.  
  412.   return NOERROR;
  413. }
  414.  
  415.  
  416. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  417.   Method:   COEnumConnectionPoints::Clone
  418.  
  419.   Summary:  The Clone member method of this IEnumConnectionPoints
  420.             interface implementation. Creates a new clone of this entire
  421.             Connection Point enumerator COM object.
  422.  
  423.   Args:     IEnumConnectionPoints** ppIEnum
  424.               Address of the caller's output pointer variable that will
  425.               receive the IEnumConnectionPoints interface pointer of the
  426.               new enumerator clone.
  427.  
  428.   Returns:  HRESULT
  429.               Standard result code. NOERROR for success.
  430. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  431. STDMETHODIMP COEnumConnectionPoints::Clone(
  432.                IEnumConnectionPoints** ppIEnum)
  433. {
  434.   HRESULT hr;
  435.   COEnumConnectionPoints* pCOEnum;
  436.  
  437.   // NULL the output variable first.
  438.   *ppIEnum = NULL;
  439.  
  440.   // Create the Clone Enumerator COM object.
  441.   pCOEnum = new COEnumConnectionPoints(m_pHostObj);
  442.   if (NULL != pCOEnum)
  443.   {
  444.     // Initialize it with same values as in this existing enumerator.
  445.     hr = pCOEnum->Init(m_cConnPts, m_paConnPts, m_iEnumIndex);
  446.     if (SUCCEEDED(hr))
  447.     {
  448.       // QueryInterface to return the requested interface pointer.
  449.       // An AddRef will be conveniently done by the QI.
  450.       if (SUCCEEDED(hr))
  451.         hr = pCOEnum->QueryInterface(
  452.                         IID_IEnumConnectionPoints,
  453.                         (PPVOID)ppIEnum);
  454.     }
  455.   }
  456.   else
  457.     hr = E_OUTOFMEMORY;
  458.  
  459.   return hr;
  460. }
  461.  
  462.  
  463. /*---------------------------------------------------------------------------
  464.   COConnectionPoint's implementation of its main COM object class
  465.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  466.   GetConnectionInterface, GetConnectionPointContainer, Advise, Unadvise,
  467.   and EnumConnections.
  468. ---------------------------------------------------------------------------*/
  469.  
  470. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  471.   Method:   COConnectionPoint::COConnectionPoint
  472.  
  473.   Summary:  COConnectionPoint Constructor.
  474.  
  475.   Args:     IUnknown* pHostObj
  476.               Pointer to IUnknown of the connectable object offering this
  477.               connection point.
  478.  
  479.   Returns:  void
  480. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  481. COConnectionPoint::COConnectionPoint(
  482.   IUnknown* pHostObj)
  483. {
  484.   // Zero the COM object's reference count.
  485.   m_cRefs = 0;
  486.  
  487.   // Remember an IUnknown pointer to the connectable object that offers
  488.   // this connection point. Since this connection point object's lifetime
  489.   // is geared to that of the connectable object there is no need to
  490.   // AddRef the following copied pointer to the connectable object.
  491.   m_pHostObj = pHostObj;
  492.  
  493.   // Initialize the Connection Point variables.
  494.   m_dwNextCookie = COOKIE_START_VALUE;
  495.   m_uiMaxIndex = 0;
  496.   m_cConnections = 0;
  497.   m_paConnections = NULL;
  498.  
  499.   return;
  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:   COConnectionPoint::~COConnectionPoint
  505.  
  506.   Summary:  COConnectionPoint Destructor.
  507.  
  508.   Args:     void
  509.  
  510.   Modifies: m_paConnections.
  511.  
  512.   Returns:  void
  513. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  514. COConnectionPoint::~COConnectionPoint(void)
  515. {
  516.   UINT i;
  517.   IUnknown* pUnk;
  518.  
  519.   if (NULL != m_paConnections)
  520.   {
  521.     // Release all the connection sink interface pointers.
  522.     for (i=0; i<m_uiMaxIndex; i++)
  523.     {
  524.       pUnk = m_paConnections[i].pUnk;
  525.       if (NULL != pUnk)
  526.         pUnk->Release();
  527.     }
  528.  
  529.     // Delete the array of interface pointers.
  530.     delete [] m_paConnections;
  531.   }
  532.  
  533.   return;
  534. }
  535.  
  536.  
  537. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  538.   Method:   COConnectionPoint::Init
  539.  
  540.   Summary:  COConnectionPoint Initialization method.  Create any
  541.             necessary arrays, structures, and subordinate objects.
  542.  
  543.   Args:     REFIID riid
  544.               Reference to the IID of the Sink interface associated with
  545.               this connection point.
  546.  
  547.   Returns:  HRESULT
  548.               Standard result code. NOERROR for success.
  549. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  550. HRESULT COConnectionPoint::Init(
  551.           REFIID riid)
  552. {
  553.   HRESULT hr = NOERROR;
  554.   CONNECTDATA* paConns;
  555.  
  556.   // Keep a copy of the reference to the IID of the sink interface
  557.   // associated with this connection point. Needed for later
  558.   // use by the GetConnectionInterface method.
  559.   m_iidSink = riid;
  560.  
  561.   // Build the initial dynamic array for connections.
  562.   paConns = new CONNECTDATA[ALLOC_CONNECTIONS];
  563.   if (NULL != paConns)
  564.   {
  565.     // Zero the array.
  566.     memset(paConns, 0, ALLOC_CONNECTIONS * sizeof(CONNECTDATA));
  567.  
  568.     // Rig this connection point object so that it will use the
  569.     // new internal array of connections.
  570.     m_uiMaxIndex = ALLOC_CONNECTIONS;
  571.     m_paConnections = paConns;
  572.   }
  573.   else
  574.     hr = E_OUTOFMEMORY;
  575.  
  576.   return (hr);
  577. }
  578.  
  579.  
  580. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  581.   Method:   COConnectionPoint::QueryInterface
  582.  
  583.   Summary:  QueryInterface of the COConnectionPoint non-delegating
  584.             IUnknown implementation.
  585.  
  586.   Args:     REFIID riid,
  587.               [in] GUID of the Interface being requested.
  588.             PPVOID ppv)
  589.               [out] Address of the caller's pointer variable that will
  590.               receive the requested interface pointer.
  591.  
  592.   Returns:  HRESULT
  593.               Standard result code. NOERROR for success.
  594. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  595. STDMETHODIMP COConnectionPoint::QueryInterface(
  596.                REFIID riid,
  597.                PPVOID ppv)
  598. {
  599.   HRESULT hr = E_NOINTERFACE;
  600.  
  601.   *ppv = NULL;
  602.  
  603.   // The IConnectionPoint interface is implemented directly in this
  604.   // COM object rather than being a nested interface implementation.
  605.   if (IID_IUnknown == riid || IID_IConnectionPoint == riid)
  606.     *ppv = (LPVOID)this;
  607.  
  608.   if (NULL != *ppv)
  609.   {
  610.     // We've handed out a pointer to the interface so obey the COM rules
  611.     // and AddRef the reference count.
  612.     ((LPUNKNOWN)*ppv)->AddRef();
  613.     hr = NOERROR;
  614.   }
  615.  
  616.   return (hr);
  617. }
  618.  
  619.  
  620. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  621.   Method:   COConnectionPoint::AddRef
  622.  
  623.   Summary:  AddRef of the COConnectionPoint non-delegating IUnknown
  624.             implementation.
  625.  
  626.   Args:     void
  627.  
  628.   Modifies: m_cRefs.
  629.  
  630.   Returns:  ULONG
  631.               New value of m_cRefs (COM object's reference count).
  632. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  633. STDMETHODIMP_(ULONG) COConnectionPoint::AddRef(void)
  634. {
  635.   ULONG cRefs;
  636.  
  637.   if (OwnThis())
  638.   {
  639.     cRefs = ++m_cRefs;
  640.  
  641.     UnOwnThis();
  642.   }
  643.  
  644.   return cRefs;
  645. }
  646.  
  647.  
  648. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  649.   Method:   COConnectionPoint::Release
  650.  
  651.   Summary:  Release of the COConnectionPoint non-delegating IUnknown
  652.             implementation.
  653.  
  654.   Args:     void
  655.  
  656.   Modifies: m_cRefs.
  657.  
  658.   Returns:  ULONG
  659.               New value of m_cRefs (COM object's reference count).
  660. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  661. STDMETHODIMP_(ULONG) COConnectionPoint::Release(void)
  662. {
  663.   ULONG cRefs;
  664.  
  665.   if (OwnThis())
  666.   {
  667.     cRefs = --m_cRefs;
  668.  
  669.     if (0 == cRefs)
  670.     {
  671.       // We artificially bump the main ref count to prevent reentrancy via
  672.       // the main object destructor. We relinquish thread ownership of this
  673.       // object prior to deleting it--a good practice.
  674.       m_cRefs++;
  675.       UnOwnThis();
  676.       delete this;
  677.     }
  678.     else
  679.       UnOwnThis();
  680.   }
  681.  
  682.   return cRefs;
  683. }
  684.  
  685.  
  686. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  687.   Method:   COConnectionPoint::GetSlot
  688.  
  689.   Summary:  An internal private utility member method to obtain a free
  690.             slot in the dynamic connections array. GetSlot will expand the
  691.             dynamic array for more entries if needed. To guarantee thread
  692.             safety, this private method should always be called within the
  693.             protection of a bracketed OwnThis, UnOwnThis pair.
  694.  
  695.   Args:     UINT* puiFreeSlot
  696.               Address of an output variable to receive the free slot index.
  697.  
  698.   Modifies: m_uiMaxIndex, m_paConnections.
  699.  
  700.   Returns:  HRESULT
  701.               Standard result code. NOERROR for success.
  702. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  703. HRESULT COConnectionPoint::GetSlot(
  704.           UINT* puiFreeSlot)
  705. {
  706.   HRESULT hr = NOERROR;
  707.   BOOL bOpen = FALSE;
  708.   UINT i;
  709.   CONNECTDATA* paConns;
  710.  
  711.   // Zero the output variable.
  712.   *puiFreeSlot = 0;
  713.  
  714.   // Loop to find an empty slot.
  715.   for (i=0; i<m_uiMaxIndex; i++)
  716.   {
  717.     if (m_paConnections[i].dwCookie == 0)
  718.     {
  719.       // We found an open empty slot.
  720.       *puiFreeSlot = i;
  721.       bOpen = TRUE;
  722.       break;
  723.     }
  724.   }
  725.  
  726.   if (!bOpen)
  727.   {
  728.     // We didn't find an existing open slot in the array--it's full.
  729.     // Expand the array by ALLOC_CONNECTIONS entries and assign the
  730.     // appropriate output index.
  731.     paConns = new CONNECTDATA[m_uiMaxIndex + ALLOC_CONNECTIONS];
  732.     if (NULL != paConns)
  733.     {
  734.       // Copy the content of the old full array to the new larger array.
  735.       for (i=0; i<m_uiMaxIndex; i++)
  736.       {
  737.         paConns[i].pUnk = m_paConnections[i].pUnk;
  738.         paConns[i].dwCookie = m_paConnections[i].dwCookie;
  739.       }
  740.  
  741.       // Zero (ie mark as empty) the expanded portion of the new array.
  742.       for (i=m_uiMaxIndex; i<m_uiMaxIndex+ALLOC_CONNECTIONS; i++)
  743.       {
  744.         paConns[i].pUnk = NULL;
  745.         paConns[i].dwCookie = 0;
  746.       }
  747.  
  748.       // New larger array is ready--delete the old array.
  749.       delete [] m_paConnections;
  750.  
  751.       // Rig the connection point to use the new larger array.
  752.       m_paConnections = paConns;
  753.  
  754.       // Assign the output free slot as first entry in new expanded area.
  755.       *puiFreeSlot = m_uiMaxIndex;
  756.  
  757.       // Calculate the new max index.
  758.       m_uiMaxIndex += ALLOC_CONNECTIONS;
  759.     }
  760.     else
  761.       hr = E_OUTOFMEMORY;
  762.   }
  763.  
  764.   return hr;
  765. }
  766.  
  767.  
  768. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  769.   Method:   COConnectionPoint::FindSlot
  770.  
  771.   Summary:  An internal private utility member method to find an existing
  772.             slot (identified by the specified dwCookie value) in the
  773.             dynamic connections array.
  774.  
  775.   Args:     DWORD dwCookie,
  776.               The connection key (cookie) to find.
  777.             UINT* puiSlot)
  778.               Address of an output variable to receive the slot index.
  779.  
  780.   Returns:  HRESULT
  781.               Standard result code. NOERROR for success.
  782. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  783. HRESULT COConnectionPoint::FindSlot(
  784.           DWORD dwCookie,
  785.           UINT* puiSlot)
  786. {
  787.   HRESULT hr = CONNECT_E_NOCONNECTION;
  788.   UINT i;
  789.  
  790.   // Loop to find the Cookie.
  791.   for (i=0; i<m_uiMaxIndex; i++)
  792.   {
  793.     if (dwCookie == m_paConnections[i].dwCookie)
  794.     {
  795.       // If a cookie match is found, assign the output slot index.
  796.       *puiSlot = i;
  797.       hr = NOERROR;
  798.       break;
  799.     }
  800.   }
  801.  
  802.   return hr;
  803. }
  804.  
  805.  
  806. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  807.   Method:   COConnectionPoint::GetConnectionInterface
  808.  
  809.   Summary:  The GetConnectionInterface member method of this
  810.             IConnectionPoint interface implementation. Called to get the
  811.             IID of the Sink interface associated with this connection
  812.             point.
  813.  
  814.   Args:     IID* piidSink
  815.               Pointer to the IID of the associated sink interface.
  816.  
  817.   Returns:  HRESULT
  818.               Standard result code. NOERROR for success.
  819. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  820. STDMETHODIMP COConnectionPoint::GetConnectionInterface(
  821.                IID* piidSink)
  822. {
  823.   HRESULT hr = NOERROR;
  824.  
  825.   if (NULL != piidSink)
  826.     *piidSink = m_iidSink;
  827.   else
  828.     hr = E_POINTER;
  829.  
  830.   return hr;
  831. }
  832.  
  833.  
  834. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  835.   Method:   COConnectionPoint::GetConnectionPointContainer
  836.  
  837.   Summary:  The GetConnectionPointContainer member method of this
  838.             IConnectionPoint interface implementation. Called to get the
  839.             connection point container that contains this connection
  840.             point.
  841.  
  842.   Args:     IConnectionPointContainer** ppConnPtCon
  843.               Address of the pointer variable that will recieve the
  844.               IConnectionPointContainer interface pointer.
  845.  
  846.   Returns:  HRESULT
  847.               Standard result code. NOERROR for success.
  848. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  849. STDMETHODIMP COConnectionPoint::GetConnectionPointContainer(
  850.                IConnectionPointContainer** ppConnPtCon)
  851. {
  852.   HRESULT hr;
  853.  
  854.   // Use QueryInterface to get the interface pointer and to perform the
  855.   // needed AddRef on the returned pointer.
  856.   hr = m_pHostObj->QueryInterface(
  857.                      IID_IConnectionPointContainer,
  858.                      (PPVOID) ppConnPtCon);
  859.  
  860.   return hr;
  861. }
  862.  
  863.  
  864. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  865.   Method:   COConnectionPoint::Advise
  866.  
  867.   Summary:  The Advise member method of this IConnectionPoint interface
  868.             implementation. Called by clients to establish a connection of
  869.             their sink to this connection point. Uses the CThreaded
  870.             OwnThis mechanism to provide mutually exclusive access by
  871.             multiple threads.
  872.  
  873.   Args:     IUnknown* pUnkSink
  874.               IUnknown interface pointer of the Sink object in the client.
  875.             DWORD* pdwCookie
  876.               Pointer to a DWORD in the client that will receive a unique
  877.               key used by the client to refer to the connection established
  878.               by this Advise call.
  879.  
  880.   Returns:  HRESULT
  881.               Standard result code. NOERROR for success.
  882. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  883. STDMETHODIMP COConnectionPoint::Advise(
  884.                IUnknown* pUnkSink,
  885.                DWORD* pdwCookie)
  886. {
  887.   HRESULT hr = NOERROR;
  888.   UINT uiFreeSlot = 0;
  889.   IUnknown* pISink = NULL;
  890.  
  891.   if (OwnThis())
  892.   {
  893.     // Zero the output connection key.
  894.     *pdwCookie = 0;
  895.  
  896.     // Get the specific associated client Sink interface that this
  897.     // connection point expects to use for notifications.
  898.     hr = pUnkSink->QueryInterface(m_iidSink, (PPVOID)&pISink);
  899.     if (SUCCEEDED(hr))
  900.     {
  901.       // Store the specific sink interface in this connection point's
  902.       // array of live connections. First find a free slot (expand the
  903.       // array if needed).
  904.       hr = GetSlot(&uiFreeSlot);
  905.       if (SUCCEEDED(hr))
  906.       {
  907.         // Assign the new slot with the connection entry.
  908.         m_paConnections[uiFreeSlot].pUnk = pISink;
  909.         m_paConnections[uiFreeSlot].dwCookie = m_dwNextCookie;
  910.  
  911.         // Assign the output Cookie value.
  912.         *pdwCookie = m_dwNextCookie;
  913.  
  914.         // Increment the Cookie counter.
  915.         m_dwNextCookie++;
  916.  
  917.         // Increment the number of live connections.
  918.         m_cConnections++;
  919.       }
  920.     }
  921.     else if (hr == E_NOINTERFACE)
  922.     {
  923.        // The sink does not support m_iidSink.
  924.        hr = CONNECT_E_CANNOTCONNECT;
  925.     }
  926.  
  927.     UnOwnThis();
  928.   }
  929.  
  930.   return hr;
  931. }
  932.  
  933.  
  934. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  935.   Method:   COConnectionPoint::Unadvise
  936.  
  937.   Summary:  The Unadvise member method of this IConnectionPoint interface
  938.             implementation. Called by clients to disconnect a connection
  939.             of their sink to this connection point. The connection is
  940.             identified by the dwCookie argument (obtained by a previous
  941.             Advise call). Uses the CThreaded OwnThis mechanism to provide
  942.             mutually exclusive access by multiple threads.
  943.  
  944.   Args:     DWORD dwCookie
  945.               Connection key that was obtained by a previous Advise call.
  946.  
  947.   Returns:  HRESULT
  948.               Standard result code. NOERROR for success.
  949. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  950. STDMETHODIMP COConnectionPoint::Unadvise(
  951.                DWORD dwCookie)
  952. {
  953.   HRESULT hr = NOERROR;
  954.   UINT uiSlot;
  955.  
  956.   if (0 != dwCookie)
  957.   {
  958.     if (OwnThis())
  959.     {
  960.       hr = FindSlot(dwCookie, &uiSlot);
  961.       if (SUCCEEDED(hr))
  962.       {
  963.         // Release the sink interface.
  964.         RELEASE_INTERFACE(m_paConnections[uiSlot].pUnk);
  965.  
  966.         // Mark the array entry as empty.
  967.         m_paConnections[uiSlot].dwCookie = 0;
  968.  
  969.         // Decrement the number of live connections.
  970.         m_cConnections--;
  971.       }
  972.  
  973.       UnOwnThis();
  974.     }
  975.   }
  976.   else
  977.     hr = E_INVALIDARG;
  978.  
  979.   return hr;
  980. }
  981.  
  982.  
  983. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  984.   Method:   COConnectionPoint::EnumConnections
  985.  
  986.   Summary:  The EnumConnections member method of this IConnectionPoint
  987.             interface implementation. Called to obtain an IEnumConnections
  988.             enumerator interface that can be used to enumerate the
  989.             connections of this connection point. Uses the CThreaded
  990.             OwnThis mechanism to ensure mutually exclusive access by
  991.             multiple threads.
  992.  
  993.   Args:     IEnumConnections** ppIEnum
  994.               Address of the caller's output pointer variable that will
  995.               receive the enumerator IEnumConnections interface pointer.
  996.  
  997.   Returns:  HRESULT
  998.               Standard result code. NOERROR for success.
  999. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1000. STDMETHODIMP COConnectionPoint::EnumConnections(
  1001.                IEnumConnections** ppIEnum)
  1002. {
  1003.   HRESULT hr = OLE_E_NOCONNECTION;
  1004.   CONNECTDATA* paConns;
  1005.   COEnumConnections* pCOEnum;
  1006.   UINT i,j;
  1007.  
  1008.   if (OwnThis())
  1009.   {
  1010.     // Zero the output enumerator interface pointer.
  1011.     *ppIEnum = NULL;
  1012.  
  1013.     if (0 != m_cConnections)
  1014.     {
  1015.       // Create an array of CONNECTDATA structures.
  1016.       paConns = new CONNECTDATA[(UINT)m_cConnections];
  1017.       if (NULL != paConns)
  1018.       {
  1019.         for (i=0, j=0; i<m_uiMaxIndex && j<m_cConnections; i++)
  1020.         {
  1021.           // Copy non-empty entries only.
  1022.           if (0 != m_paConnections[i].dwCookie)
  1023.           {
  1024.             // Assign the occupied entry.
  1025.             paConns[j].pUnk = (IUnknown*)m_paConnections[i].pUnk;
  1026.             paConns[j].dwCookie = m_paConnections[i].dwCookie;
  1027.             j++;
  1028.           }
  1029.         }
  1030.  
  1031.         // Create a new COM object for enumerating connections. Pass
  1032.         // 'this' as a pHostObj pointer used later to ensure the host
  1033.         // connection point object stays alive as long as the enumerator
  1034.         // that enumerates connections to that connection point.
  1035.         pCOEnum = new COEnumConnections(this);
  1036.         if (NULL != pCOEnum)
  1037.         {
  1038.           // Use the previously constructed (paConns) array of connections
  1039.           // to init the new COEnumConnections COM object. The Init will
  1040.           // build yet another internal copy of this array. Set the
  1041.           // initial enumerator index to 0.
  1042.           hr = pCOEnum->Init(m_cConnections, paConns, 0);
  1043.  
  1044.           // QueryInterface to return the requested interface pointer.
  1045.           // An AddRef will be conveniently done by the QI.
  1046.           if (SUCCEEDED(hr))
  1047.             hr = pCOEnum->QueryInterface(
  1048.                             IID_IEnumConnections,
  1049.                             (PPVOID)ppIEnum);
  1050.         }
  1051.         else
  1052.           hr = E_OUTOFMEMORY;
  1053.  
  1054.         // We're done with the locally constructed array copy--delete it.
  1055.         delete [] paConns;
  1056.       }
  1057.       else
  1058.         hr = E_OUTOFMEMORY;
  1059.     }
  1060.  
  1061.     UnOwnThis();
  1062.   }
  1063.  
  1064.   return hr;
  1065. }
  1066.  
  1067.  
  1068. /*---------------------------------------------------------------------------
  1069.   COEnumConnections's implementation of its main COM object class
  1070.   including Constructor, Destructor, QueryInterface, AddRef, Release,
  1071.   Next, Skip, Reset, and Clone.
  1072. ---------------------------------------------------------------------------*/
  1073.  
  1074. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1075.   Method:   COEnumConnections::COEnumConnections
  1076.  
  1077.   Summary:  COEnumConnections Constructor.
  1078.  
  1079.   Args:     IUnknown* pHostObj
  1080.               Pointer to IUnknown interface of the host Connection Point
  1081.               COM object whose connections are being enumerated.
  1082.  
  1083.   Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnections,
  1084.             and m_paConnections.
  1085.  
  1086.   Returns:  void
  1087. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1088. COEnumConnections::COEnumConnections(
  1089.   IUnknown* pHostObj)
  1090. {
  1091.   // Zero the COM object's reference count.
  1092.   m_cRefs = 0;
  1093.  
  1094.   // Assign the Host Object pointer.
  1095.   m_pHostObj = pHostObj;
  1096.  
  1097.   // Initialize the Connection Point enumerator variables.
  1098.   m_iEnumIndex = 0;
  1099.   m_cConnections = 0;
  1100.   m_paConnections = NULL;
  1101.  
  1102.   return;
  1103. }
  1104.  
  1105.  
  1106. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1107.   Method:   COEnumConnections::~COEnumConnections
  1108.  
  1109.   Summary:  COEnumConnections Destructor.
  1110.  
  1111.   Args:     void
  1112.  
  1113.   Modifies: m_paConnections.
  1114.  
  1115.   Returns:  void
  1116. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1117. COEnumConnections::~COEnumConnections(void)
  1118. {
  1119.   if (NULL != m_paConnections)
  1120.   {
  1121.     UINT i;
  1122.  
  1123.     // Release all the connected sink interface pointers.
  1124.     for (i=0; i<m_cConnections; i++)
  1125.       m_paConnections[i].pUnk->Release();
  1126.  
  1127.     // Delete the array of connections.
  1128.     delete [] m_paConnections;
  1129.   }
  1130.  
  1131.   return;
  1132. }
  1133.  
  1134.  
  1135. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1136.   Method:   COEnumConnections::Init
  1137.  
  1138.   Summary:  COEnumConnections Initialization method.  Create any
  1139.             necessary arrays, structures, and objects.
  1140.  
  1141.   Args:     ULONG cConnections
  1142.               Number of Connections.
  1143.             CONNECTDATA* paConnections,
  1144.               Pointer to array of connections.
  1145.             ULONG iEnumIndex
  1146.               The Enumerator index initial value.
  1147.  
  1148.   Modifies: m_cConnections, m_paConnections, m_pHostObj, m_iEnumIndex.
  1149.  
  1150.   Returns:  HRESULT
  1151.               Standard result code. NOERROR for success.
  1152. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1153. HRESULT COEnumConnections::Init(
  1154.           ULONG cConnections,
  1155.           CONNECTDATA* paConnections,
  1156.           ULONG iEnumIndex)
  1157. {
  1158.   HRESULT hr = NOERROR;
  1159.   UINT i;
  1160.  
  1161.   // Remember the number of live Connections.
  1162.   m_cConnections = cConnections;
  1163.  
  1164.   // Remember the initial enumerator index.
  1165.   m_iEnumIndex = iEnumIndex;
  1166.  
  1167.   // Create a copy of the array of connections and keep it inside
  1168.   // this enumerator COM object.
  1169.   m_paConnections = new CONNECTDATA [(UINT) cConnections];
  1170.  
  1171.   // Fill the array copy with the connection data from the connections
  1172.   // array passed. AddRef for each new sink Interface pointer copy made.
  1173.   if (NULL != m_paConnections)
  1174.   {
  1175.     for (i=0; i<cConnections; i++)
  1176.     {
  1177.       m_paConnections[i] = paConnections[i];
  1178.       m_paConnections[i].pUnk->AddRef();
  1179.     }
  1180.   }
  1181.   else
  1182.     hr = E_OUTOFMEMORY;
  1183.  
  1184.   return (hr);
  1185. }
  1186.  
  1187.  
  1188. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1189.   Method:   COEnumConnections::QueryInterface
  1190.  
  1191.   Summary:  QueryInterface of the COEnumConnections non-delegating
  1192.             IUnknown implementation.
  1193.  
  1194.   Args:     REFIID riid,
  1195.               [in] GUID of the Interface being requested.
  1196.             PPVOID ppv)
  1197.               [out] Address of the caller's pointer variable that will
  1198.               receive the requested interface pointer.
  1199.  
  1200.   Returns:  HRESULT
  1201.               Standard result code. NOERROR for success.
  1202. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1203. STDMETHODIMP COEnumConnections::QueryInterface(
  1204.                REFIID riid,
  1205.                PPVOID ppv)
  1206. {
  1207.   HRESULT hr = E_NOINTERFACE;
  1208.  
  1209.   *ppv = NULL;
  1210.  
  1211.   // The IEnumConnections interface is implemented directly in
  1212.   // this COM object rather than being a nested interface implementation.
  1213.   if (IID_IUnknown == riid || IID_IEnumConnections == riid)
  1214.     *ppv = (LPVOID)this;
  1215.  
  1216.   if (NULL != *ppv)
  1217.   {
  1218.     // We've handed out a pointer to the interface so obey the COM rules
  1219.     // and AddRef the reference count.
  1220.     ((LPUNKNOWN)*ppv)->AddRef();
  1221.     hr = NOERROR;
  1222.   }
  1223.  
  1224.   return (hr);
  1225. }
  1226.  
  1227.  
  1228. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1229.   Method:   COEnumConnections::AddRef
  1230.  
  1231.   Summary:  AddRef of the COEnumConnections non-delegating IUnknown
  1232.             implementation.
  1233.  
  1234.   Args:     void
  1235.  
  1236.   Modifies: m_cRefs.
  1237.  
  1238.   Returns:  ULONG
  1239.               New value of m_cRefs (COM object's reference count).
  1240. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1241. STDMETHODIMP_(ULONG) COEnumConnections::AddRef(void)
  1242. {
  1243.   ULONG cRefs;
  1244.  
  1245.   cRefs = ++m_cRefs;
  1246.  
  1247.   // Also AddRef the host object to ensure it stays around as long as
  1248.   // this enumerator.
  1249.   m_pHostObj->AddRef();
  1250.  
  1251.   return cRefs;
  1252. }
  1253.  
  1254.  
  1255. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1256.   Method:   COEnumConnections::Release
  1257.  
  1258.   Summary:  Release of the COEnumConnections non-delegating IUnknown
  1259.             implementation.
  1260.  
  1261.   Args:     void
  1262.  
  1263.   Modifies: m_cRefs.
  1264.  
  1265.   Returns:  ULONG
  1266.               New value of m_cRefs (COM object's reference count).
  1267. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1268. STDMETHODIMP_(ULONG) COEnumConnections::Release(void)
  1269. {
  1270.   ULONG cRefs;
  1271.  
  1272.   // Pass this release along to the Host object being enumerated.
  1273.   m_pHostObj->Release();
  1274.  
  1275.   cRefs = --m_cRefs;
  1276.  
  1277.   if (0 == cRefs)
  1278.   {
  1279.     // We artificially bump the main ref count to prevent reentrancy via
  1280.     // the main object destructor.
  1281.     m_cRefs++;
  1282.     delete this;
  1283.   }
  1284.  
  1285.   return cRefs;
  1286. }
  1287.  
  1288.  
  1289. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1290.   Method:   COEnumConnections::Next
  1291.  
  1292.   Summary:  The Next member method of this IEnumConnections interface
  1293.             implementation. Called by outside clients of a
  1294.             COEnumConnections object to request a number of next
  1295.             connections be returned in an array supplied by the caller.
  1296.  
  1297.   Args:     ULONG cReq
  1298.               Number of connection points requested for return (starting at
  1299.               the current Enumerator index).
  1300.             CONNECTDATA* paConnections,
  1301.               Pointer to a caller's output array that will receive the
  1302.               enumerated connection data structures.
  1303.             ULONG* pcEnumerated)
  1304.               Pointer to a ULONG variable that will contain the number of
  1305.               connection points actually enumerated by this call.
  1306.  
  1307.   Returns:  HRESULT
  1308.               Standard result code. NOERROR for success.
  1309. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1310. STDMETHODIMP COEnumConnections::Next(
  1311.                ULONG cReq,
  1312.                CONNECTDATA* paConnections,
  1313.                ULONG* pcEnumerated)
  1314. {
  1315.   HRESULT hr = NOERROR;
  1316.   ULONG cRet = 0;
  1317.  
  1318.   // Make sure the argument values passed are valid.
  1319.   if (NULL != m_paConnections)
  1320.   {
  1321.     if (NULL != paConnections)
  1322.     {
  1323.       if (m_iEnumIndex < m_cConnections)
  1324.       {
  1325.         if (NULL != pcEnumerated)
  1326.           *pcEnumerated = 0L;
  1327.         else
  1328.           if (1L != cReq)
  1329.             hr = E_POINTER;
  1330.       }
  1331.       else
  1332.         hr = S_FALSE;
  1333.     }
  1334.     else
  1335.       hr = E_POINTER;
  1336.   }
  1337.   else
  1338.     hr = S_FALSE;
  1339.  
  1340.   if (SUCCEEDED(hr))
  1341.   {
  1342.     // Starting at the current Enumerator index, loop to assign the
  1343.     // requested number of output connection data structures.
  1344.     for (; m_iEnumIndex < m_cConnections && cReq > 0;
  1345.            paConnections++, m_iEnumIndex++, cRet++, cReq--)
  1346.     {
  1347.       // Because we are assigning a copy of a connection's data, AddRef
  1348.       // its sink interface pointer.
  1349.       if (NULL != m_paConnections[m_iEnumIndex].pUnk)
  1350.         m_paConnections[m_iEnumIndex].pUnk->AddRef();
  1351.  
  1352.       // Assign a connection's data from the inside Enumerator array to
  1353.       // the specified output receiving array.
  1354.       *paConnections = m_paConnections[m_iEnumIndex];
  1355.     }
  1356.  
  1357.     // Assign the output number of connections enumerated.
  1358.     if (NULL != pcEnumerated)
  1359.       *pcEnumerated = cRet;
  1360.   }
  1361.  
  1362.   return hr;
  1363. }
  1364.  
  1365.  
  1366. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1367.   Method:   COEnumConnections::Skip
  1368.  
  1369.   Summary:  The Skip member method of this IEnumConnections interface
  1370.             implementation. Starting at the current Enumerator index, skip
  1371.             the specified number of connection items.
  1372.  
  1373.   Args:     ULONG cSkip
  1374.               Number of Connection items to skip.
  1375.  
  1376.   Returns:  HRESULT
  1377.               Standard result code. NOERROR for success.
  1378. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1379. STDMETHODIMP COEnumConnections::Skip(
  1380.                ULONG cSkip)
  1381. {
  1382.   HRESULT hr = NOERROR;
  1383.  
  1384.   // If there really is a connection array and the requested
  1385.   // amount of skip does not exceed the number of connections,
  1386.   // then bump the index by the requested skip amount.
  1387.   if (NULL != m_paConnections && (m_iEnumIndex + cSkip) < m_cConnections)
  1388.     m_iEnumIndex += cSkip;
  1389.   else
  1390.     hr = S_FALSE;
  1391.  
  1392.   return hr;
  1393. }
  1394.  
  1395.  
  1396. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1397.   Method:   COEnumConnections::Reset
  1398.  
  1399.   Summary:  The Reset member method of the IEnumConnections interface
  1400.             implementation. Resets the Enumeration index to the first
  1401.             connection item in the array.
  1402.  
  1403.   Args:     void.
  1404.  
  1405.   Returns:  HRESULT
  1406.               Standard result code. NOERROR for success.
  1407. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1408. STDMETHODIMP COEnumConnections::Reset(
  1409.                void)
  1410. {
  1411.   // Zero the main Enumerator index.
  1412.   m_iEnumIndex = 0;
  1413.  
  1414.   return NOERROR;
  1415. }
  1416.  
  1417.  
  1418. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1419.   Method:   COEnumConnections::Clone
  1420.  
  1421.   Summary:  The Clone member method of this IEnumConnections interface
  1422.             implementation. Creates a new clone of this entire Connection
  1423.             enumerator COM object and returns a pointer to its
  1424.             IEnumConnections interface.
  1425.  
  1426.   Args:     IEnumConnections** ppIEnum
  1427.               Address of the caller's output pointer variable that will
  1428.               receive the IEnumConnections interface pointer of the
  1429.               new enumerator clone.
  1430.  
  1431.   Returns:  HRESULT
  1432.               Standard result code. NOERROR for success.
  1433. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1434. STDMETHODIMP COEnumConnections::Clone(
  1435.                IEnumConnections** ppIEnum)
  1436. {
  1437.   HRESULT hr;
  1438.   COEnumConnections* pCOEnum;
  1439.  
  1440.   // NULL the output variable first.
  1441.   *ppIEnum = NULL;
  1442.  
  1443.   // Create the Clone Enumerator COM object.
  1444.   pCOEnum = new COEnumConnections(m_pHostObj);
  1445.   if (NULL != pCOEnum)
  1446.   {
  1447.     // Initialize it with same values as in this existing enumerator.
  1448.     hr = pCOEnum->Init(m_cConnections, m_paConnections, m_iEnumIndex);
  1449.     if (SUCCEEDED(hr))
  1450.     {
  1451.       // QueryInterface to return the requested interface pointer.
  1452.       // An AddRef will be conveniently done by the QI.
  1453.       if (SUCCEEDED(hr))
  1454.         hr = pCOEnum->QueryInterface(
  1455.                         IID_IEnumConnections,
  1456.                         (PPVOID)ppIEnum);
  1457.     }
  1458.   }
  1459.   else
  1460.     hr = E_OUTOFMEMORY;
  1461.  
  1462.   return hr;
  1463. }
  1464.