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