home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap05 / polyline / iconnpt.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  7KB  |  327 lines

  1. /*
  2.  * ICONNPT.CPP
  3.  * Polyline Component Chapter 5
  4.  *
  5.  * Implementation of CImpIConnectionPoint for the Polyline object
  6.  * as well as CConnectionPoint.
  7.  *
  8.  *
  9.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Microsoft
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "polyline.h"
  18.  
  19.  
  20. /*
  21.  * CImpIConnPtCont:CImpIConnPtCont
  22.  * CImpIConnPtCont::~CImpIConnPtCont
  23.  *
  24.  * Constructor Parameters:
  25.  *  pObj            PCPolyline pointing to the object we live in.
  26.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  27.  */
  28.  
  29. CImpIConnPtCont::CImpIConnPtCont(PCPolyline pObj
  30.     , LPUNKNOWN pUnkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_pUnkOuter=pUnkOuter;
  35.     return;
  36.     }
  37.  
  38.  
  39. CImpIConnPtCont::~CImpIConnPtCont(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CImpIConnPtCont::QueryInterface
  49.  * CImpIConnPtCont::AddRef
  50.  * CImpIConnPtCont::Release
  51.  */
  52.  
  53. STDMETHODIMP CImpIConnPtCont::QueryInterface(REFIID riid, PPVOID ppv)
  54.     {
  55.     return m_pUnkOuter->QueryInterface(riid, ppv);
  56.     }
  57.  
  58. STDMETHODIMP_(ULONG) CImpIConnPtCont::AddRef(void)
  59.     {
  60.     ++m_cRef;
  61.     return m_pUnkOuter->AddRef();
  62.     }
  63.  
  64. STDMETHODIMP_(ULONG) CImpIConnPtCont::Release(void)
  65.     {
  66.     --m_cRef;
  67.     return m_pUnkOuter->Release();
  68.     }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.  * CImpIConnPtCont::EnumConnectionPoints
  76.  *
  77.  * Purpose:
  78.  *  Not implemented.
  79.  *
  80.  * Return Value:
  81.  *  HRESULT         E_NOTIMPL
  82.  */
  83.  
  84. STDMETHODIMP CImpIConnPtCont::EnumConnectionPoints
  85.     (LPENUMCONNECTIONPOINTS *ppEnum)
  86.     {
  87.     *ppEnum=NULL;
  88.     return ResultFromScode(E_NOTIMPL);
  89.     }
  90.  
  91.  
  92.  
  93. /*
  94.  * CImpIConnPtCont::FindConnectionPoint
  95.  *
  96.  * Purpose:
  97.  *  Returns a pointer to the IConnectionPoint for a given
  98.  *  outgoing IID.
  99.  *
  100.  * Parameters:
  101.  *  riid            REFIID of the outgoing interface for which
  102.  *                  a connection point is desired.
  103.  *  ppCP            IConnectionPoint ** in which to return
  104.  *                  the pointer after calling AddRef.
  105.  *
  106.  * Return Value:
  107.  *  HRESULT         NOERROR if the connection point is found,
  108.  *                  E_NOINTERFACE if it's not supported.
  109.  */
  110.  
  111. STDMETHODIMP CImpIConnPtCont::FindConnectionPoint(REFIID riid
  112.     , IConnectionPoint **ppCP)
  113.     {
  114.     *ppCP=NULL;
  115.  
  116.     if (IID_IPolylineAdviseSink5==riid)
  117.         {
  118.         return m_pObj->m_pConnPt->QueryInterface
  119.             (IID_IConnectionPoint, (PPVOID)ppCP);
  120.         }
  121.  
  122.     return ResultFromScode(E_NOINTERFACE);
  123.     }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. //CConnectionPoint implementation
  130.  
  131. /*
  132.  * CConnectionPoint::CConnectionPoint
  133.  * CConnectionPoint::~CConnectionPoint
  134.  *
  135.  * Parameters (Constructor):
  136.  *  pObj            PCPolyline of the object we're in.  We can
  137.  *                  query this for the IConnectionPointContainer
  138.  *                  interface we might need.
  139.  */
  140.  
  141. CConnectionPoint::CConnectionPoint(PCPolyline pObj)
  142.     {
  143.     m_cRef=0;
  144.  
  145.     /*
  146.      * Our lifetime is controlled by the connectable object itself,
  147.      * although other external clients will call AddRef and Release.
  148.      * Since we're nested in the connectable object's lifetime,
  149.      * there's no need to call AddRef on pObj.
  150.      */
  151.     m_pObj=pObj;
  152.     return;
  153.     }
  154.  
  155. CConnectionPoint::~CConnectionPoint(void)
  156.     {
  157.     ReleaseInterface(m_pObj->m_pAdv);
  158.     return;
  159.     }
  160.  
  161.  
  162.  
  163. /*
  164.  * CConnectionPoint::QueryInterface
  165.  * CConnectionPoint::AddRef
  166.  * CConnectionPoint::Release
  167.  *
  168.  * Purpose:
  169.  *  Non-delegating IUnknown members for CConnectionPoint.
  170.  */
  171.  
  172. STDMETHODIMP CConnectionPoint::QueryInterface(REFIID riid
  173.     , LPVOID *ppv)
  174.     {
  175.     *ppv=NULL;
  176.  
  177.     if (IID_IUnknown==riid || IID_IConnectionPoint==riid)
  178.         *ppv=(LPVOID)this;
  179.  
  180.     if (NULL!=*ppv)
  181.         {
  182.         ((LPUNKNOWN)*ppv)->AddRef();
  183.         return NOERROR;
  184.         }
  185.  
  186.     return ResultFromScode(E_NOINTERFACE);
  187.     }
  188.  
  189. STDMETHODIMP_(ULONG) CConnectionPoint::AddRef(void)
  190.     {
  191.     return ++m_cRef;
  192.     }
  193.  
  194. STDMETHODIMP_(ULONG) CConnectionPoint::Release(void)
  195.     {
  196.     if (0!=--m_cRef)
  197.         return m_cRef;
  198.  
  199.     delete this;
  200.     return 0;
  201.     }
  202.  
  203.  
  204.  
  205. /*
  206.  * CConnectionPoint::GetConnectionInterface
  207.  *
  208.  * Purpose:
  209.  *  Returns the IID of the outgoing interface supported through
  210.  *  this connection point.
  211.  *
  212.  * Parameters:
  213.  *  pIID            IID * in which to store the IID.
  214.  */
  215.  
  216. STDMETHODIMP CConnectionPoint::GetConnectionInterface(IID *pIID)
  217.     {
  218.     if (NULL==pIID)
  219.         return ResultFromScode(E_POINTER);
  220.  
  221.     *pIID=IID_IPolylineAdviseSink5;
  222.     return NOERROR;
  223.     }
  224.  
  225.  
  226.  
  227. /*
  228.  * CConnectionPoint::GetConnectionPointContainer
  229.  *
  230.  * Purpose:
  231.  *  Returns a pointer to the IConnectionPointContainer that
  232.  *  is manageing this connection point.
  233.  *
  234.  * Parameters:
  235.  *  ppCPC           IConnectionPointContainer ** in which to return
  236.  *                  the pointer after calling AddRef.
  237.  */
  238.  
  239. STDMETHODIMP CConnectionPoint::GetConnectionPointContainer
  240.     (IConnectionPointContainer **ppCPC)
  241.     {
  242.     return m_pObj->QueryInterface(IID_IConnectionPointContainer
  243.         , (void **)ppCPC);
  244.     }
  245.  
  246.  
  247.  
  248. /*
  249.  * CConnectionPoint::Advise
  250.  *
  251.  * Purpose:
  252.  *  Provides this connection point with a notification sink to
  253.  *  call whenever the appropriate outgoing function/event occurs.
  254.  *
  255.  * Parameters:
  256.  *  pUnkSink        LPUNKNOWN to the sink to notify.  The connection
  257.  *                  point must QueryInterface on this pointer to obtain
  258.  *                  the proper interface to call.  The connection
  259.  *                  point must also insure that any pointer held has
  260.  *                  a reference count (QueryInterface will do it).
  261.  *  pdwCookie       DWORD * in which to store the connection key for
  262.  *                  later calls to Unadvise.
  263.  */
  264.  
  265. STDMETHODIMP CConnectionPoint::Advise(LPUNKNOWN pUnkSink
  266.     , DWORD *pdwCookie)
  267.     {
  268.     IPolylineAdviseSink5   *pSink;
  269.  
  270.     *pdwCookie=0;
  271.  
  272.     //Only allow one connection
  273.     if (NULL!=m_pObj->m_pAdv)
  274.         return ResultFromScode(CONNECT_E_ADVISELIMIT);
  275.  
  276.     //Check for the right interface on the sink.
  277.     if (FAILED(pUnkSink->QueryInterface(IID_IPolylineAdviseSink5
  278.         , (PPVOID)&pSink)))
  279.         return ResultFromScode(CONNECT_E_CANNOTCONNECT);
  280.  
  281.     *pdwCookie=ADVISEKEY;
  282.     m_pObj->m_pAdv=pSink;
  283.     return NOERROR;
  284.     }
  285.  
  286.  
  287.  
  288. /*
  289.  * CConnectionPoint::Unadvise
  290.  *
  291.  * Purpose:
  292.  *  Terminates the connection to the notification sink identified
  293.  *  with dwCookie (that was returned from Advise).  The connection
  294.  *  point has to Release any held pointers for that sink.
  295.  *
  296.  * Parameters:
  297.  *  dwCookie        DWORD connection key from Advise.
  298.  */
  299.  
  300. STDMETHODIMP CConnectionPoint::Unadvise(DWORD dwCookie)
  301.     {
  302.     if (0==dwCookie)
  303.         return ResultFromScode(E_INVALIDARG);
  304.  
  305.     if (ADVISEKEY!=dwCookie)
  306.         ResultFromScode(CONNECT_E_NOCONNECTION);
  307.  
  308.     ReleaseInterface(m_pObj->m_pAdv);
  309.     return NOERROR;
  310.     }
  311.  
  312.  
  313.  
  314. /*
  315.  * CConnectionPoint::EnumConnections
  316.  *
  317.  * Purpose:
  318.  *  Not implemented.
  319.  */
  320.  
  321. STDMETHODIMP CConnectionPoint::EnumConnections
  322.     (LPENUMCONNECTIONS *ppEnum)
  323.     {
  324.     *ppEnum=NULL;
  325.     return ResultFromScode(E_NOTIMPL);
  326.     }
  327.