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 / chap24 / patron / connect.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  7KB  |  291 lines

  1. /*
  2.  * CONNECT.CPP
  3.  * Patron Chapter 24
  4.  *
  5.  * Helper functions for working with connection points on objects
  6.  * and the event sets of those objects.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * InterfaceConnect
  21.  *
  22.  * Purpose:
  23.  *  Connects some sink interface of a given IID to an object.
  24.  *
  25.  * Parameters:
  26.  *  pObj            LPUNKNOWN of the object to which we connect
  27.  *  riid            REFIID of the interface of the sink
  28.  *  pIUnknownSink   LPUNKNOWN of the caller's sink interface that
  29.  *                  is actually of the type matching riid
  30.  *  pdwConn         LPDWORD in which to return the connection key
  31.  *
  32.  * Return Value:
  33.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  34.  */
  35.  
  36. BOOL InterfaceConnect(LPUNKNOWN pObj, REFIID riid
  37.     , LPUNKNOWN pIUnknownSink, LPDWORD pdwConn)
  38.     {
  39.     HRESULT                     hr;
  40.     LPCONNECTIONPOINTCONTAINER  pCPC;
  41.     LPCONNECTIONPOINT           pCP;
  42.  
  43.     if (NULL==pObj || NULL==pIUnknownSink || NULL==pdwConn)
  44.         return FALSE;
  45.  
  46.     hr=pObj->QueryInterface(IID_IConnectionPointContainer
  47.         , (PPVOID)&pCPC);
  48.  
  49.     if (FAILED(hr))
  50.         return FALSE;
  51.  
  52.     hr=pCPC->FindConnectionPoint(riid, &pCP);
  53.  
  54.     if (SUCCEEDED(hr))
  55.         {
  56.         hr=pCP->Advise(pIUnknownSink, pdwConn);
  57.         pCP->Release();
  58.         }
  59.  
  60.     pCPC->Release();
  61.     return SUCCEEDED(hr);
  62.     }
  63.  
  64.  
  65.  
  66. /*
  67.  * InterfaceDisconnect
  68.  *
  69.  * Purpose:
  70.  *  Disconnects a prior connection to an object.
  71.  *
  72.  * Parameters:
  73.  *  pObj            LPUNKNOWN of the object from which to disconnect
  74.  *  riid            REFIID of the interface of the sink
  75.  *  pdwConn         LPDWORD containing the key returned by
  76.  *                  InterfaceConnect.  This function will zero the
  77.  *                  key on diconnect.
  78.  *
  79.  * Return Value:
  80.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  81.  */
  82.  
  83. BOOL InterfaceDisconnect(LPUNKNOWN pObj, REFIID riid
  84.     , LPDWORD pdwConn)
  85.     {
  86.     HRESULT                     hr;
  87.     LPCONNECTIONPOINTCONTAINER  pCPC;
  88.     LPCONNECTIONPOINT           pCP;
  89.  
  90.     if (NULL==pObj || NULL==pdwConn)
  91.         return FALSE;
  92.  
  93.     if (0==*pdwConn)
  94.         return FALSE;
  95.  
  96.     hr=pObj->QueryInterface(IID_IConnectionPointContainer
  97.         , (PPVOID)&pCPC);
  98.  
  99.     if (FAILED(hr))
  100.         return FALSE;
  101.  
  102.     hr=pCPC->FindConnectionPoint(riid, &pCP);
  103.  
  104.     if (SUCCEEDED(hr))
  105.         {
  106.         hr=pCP->Unadvise(*pdwConn);
  107.  
  108.         if (SUCCEEDED(hr))
  109.             *pdwConn=0L;
  110.  
  111.         pCP->Release();
  112.         }
  113.  
  114.     pCPC->Release();
  115.     return SUCCEEDED(hr);
  116.     }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  * ObjectTypeInfo
  125.  *
  126.  * Purpose:
  127.  *  Retrieves the ITypeInfo for the entire object from which
  128.  *  one can learn the IID of the event set and navigate to the
  129.  *  ITypeInfo for events, among other things.
  130.  *
  131.  * Parameters:
  132.  *  pObj            LPUNKNOWN of the object
  133.  *  ppITypeInfo     LPTYPEINFO * in which to return the ITypeInfo
  134.  *                  interface for the object's events.
  135.  *
  136.  * Return Value:
  137.  *  BOOL            TRUE if successful, FALSE otherwise.
  138.  */
  139.  
  140. BOOL ObjectTypeInfo(LPUNKNOWN pObj, LPTYPEINFO *ppITypeInfo)
  141.     {
  142.     HRESULT             hr;
  143.     LPPROVIDECLASSINFO  pIProvideClassInfo;
  144.     
  145.     if (NULL==pObj || NULL==ppITypeInfo)
  146.         return FALSE;
  147.  
  148.     *ppITypeInfo=NULL;
  149.  
  150.     /*
  151.      * Get the object's IProvideClassInfo and call the GetClassInfo
  152.      * method therein.  This will give us back the ITypeInfo for
  153.      * the entire object.
  154.      */
  155.  
  156.     hr=pObj->QueryInterface(IID_IProvideClassInfo
  157.         , (PPVOID)&pIProvideClassInfo);
  158.  
  159.     if (FAILED(hr))
  160.         return FALSE;
  161.  
  162.     hr=pIProvideClassInfo->GetClassInfo(ppITypeInfo);
  163.     pIProvideClassInfo->Release();
  164.  
  165.     return SUCCEEDED(hr);
  166.     }
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * ObjectTypeInfoEvents
  173.  *
  174.  * Purpose:
  175.  *  Retrieves the events type information from an object.  This is
  176.  *  defined to be the "default source" interface in the object's
  177.  *  type library.
  178.  *
  179.  * Parameters:
  180.  *  pObj            LPUNKNOWN of the object
  181.  *  ppITypeInfo     LPTYPEINFO * in which to return the ITypeInfo
  182.  *                  interface for the object's events.
  183.  *
  184.  * Return Value:
  185.  *  BOOL            TRUE if the event type lib exists, FALSE
  186.  *                  if not or on any other error.
  187.  */
  188.  
  189. BOOL ObjectTypeInfoEvents(LPUNKNOWN pObj, LPTYPEINFO *ppITypeInfo)
  190.     {
  191.     HRESULT             hr;
  192.     LPTYPEINFO          pITypeInfoAll;
  193.     LPTYPEATTR          pTA;
  194.  
  195.     if (NULL==pObj || NULL==ppITypeInfo)
  196.         return FALSE;
  197.  
  198.     if (!ObjectTypeInfo(pObj, &pITypeInfoAll))
  199.         return FALSE;
  200.  
  201.     /*
  202.      * We have the object's overall ITypeInfo in pITypeInfoAll.
  203.      * Now get the type attributes which will tell us the number of
  204.      * individual interfaces in this type library.  We then loop
  205.      * through the "implementation types" for all those interfaces
  206.      * calling GetImplTypeFlags, looking for the default source.
  207.      */
  208.  
  209.     *ppITypeInfo=NULL;  //Use this to determine success
  210.  
  211.     if (SUCCEEDED(pITypeInfoAll->GetTypeAttr(&pTA)))
  212.         {
  213.         UINT        i;
  214.         int         iFlags;
  215.  
  216.         for (i=0; i < pTA->cImplTypes; i++)
  217.             {
  218.             //Get the implementation type for this interface
  219.             hr=pITypeInfoAll->GetImplTypeFlags(i, &iFlags);
  220.  
  221.             if (FAILED(hr))
  222.                 continue;
  223.  
  224.             if ((iFlags & IMPLTYPEFLAG_FDEFAULT)
  225.                 && (iFlags & IMPLTYPEFLAG_FSOURCE))
  226.                 {
  227.                 HREFTYPE    hRefType=NULL;
  228.  
  229.                 /*
  230.                  * This is the interface we want.  Get a handle to
  231.                  * the type description from which we can then get
  232.                  * the ITypeInfo.
  233.                  */
  234.                 pITypeInfoAll->GetRefTypeOfImplType(i, &hRefType);
  235.                 hr=pITypeInfoAll->GetRefTypeInfo(hRefType
  236.                     , ppITypeInfo);
  237.  
  238.                 break;
  239.                 }
  240.             }
  241.  
  242.         pITypeInfoAll->ReleaseTypeAttr(pTA);
  243.         }
  244.  
  245.     pITypeInfoAll->Release();
  246.     return (NULL!=*ppITypeInfo);
  247.     }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. /*
  255.  * ObjectEventsIID
  256.  *
  257.  * Purpose:
  258.  *  Determines the IID of the object's events interface so we
  259.  *  can ask for the right IConnectionPoint for events.
  260.  *
  261.  * Parameters:
  262.  *  pObj            LPUNKNOWN of the object
  263.  *  piid            IID * in which to return the IID for events
  264.  *
  265.  * Return Value:
  266.  *  BOOL            TRUE if successful, FALSE otherwise.
  267.  */
  268.  
  269. BOOL ObjectEventsIID(LPUNKNOWN pObj, IID *piid)
  270.     {
  271.     HRESULT             hr;
  272.     LPTYPEINFO          pITypeInfo;
  273.     LPTYPEATTR          pTA;
  274.  
  275.     *piid=CLSID_NULL;
  276.  
  277.     if (!ObjectTypeInfoEvents(pObj, &pITypeInfo))
  278.         return FALSE;
  279.  
  280.     hr=pITypeInfo->GetTypeAttr(&pTA);
  281.  
  282.     if (SUCCEEDED(hr))
  283.         {
  284.         *piid=pTA->guid;
  285.         pITypeInfo->ReleaseTypeAttr(pTA);
  286.         }
  287.  
  288.     pITypeInfo->Release();
  289.     return SUCCEEDED(hr);
  290.     }
  291.