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