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