Connectable objects

Connectable objects are the objects containing one or more event callbacks. Server should define the class or interface definition using IDL or type library. Client implements this object and connects the object pointer to server. The server then calls these events as required.

Connectable objects of WebBrowser

Connectable interfaces are often denoted as source interface. Here are two event interfaces defined by InternetExplorer/IWebBrowser2. DWebBrowserEvents2 and DWebBrowserEvents are the two connection point available. Here we will see how we can get there connection point details using various interfaces.

[
  uuid(8856F961-340A-11D0-A96B-00C04FD705A2), // v.2 clsid CLSID_WebBrowser
  control,
  helpstring("WebBrowser Control")
]

coclass WebBrowser 
{
  [default]         interface     IWebBrowser2;
                    interface     IWebBrowser;
  [default, source] dispinterface DWebBrowserEvents2;
  [source]          dispinterface DWebBrowserEvents;
}

Connectable interfaces

COM/DCOM uses set of four interfaces to know the connection point and connection details of the server.

  • IConnectionPointContainer
  • IEnumConnectionPoints
  • IConnectionPoint
  • IEnumConnections

These interfaces are very generic and can fit into any connectable object.

IConnectionPointContainer

COM/DCOM server contains a one or more connection point interfaces and this provides interfaces to get the details of the individual connection points.

  • EnumConnectionPoints - Returns the enumerator object which can be iterated to know the
  • FindConnectionPoint - Takes an IID and returns a connection point object if it is available in the server container
interface IConnectionPointContainer : IUnknown
{

    HRESULT EnumConnectionPoints
    (
        [out]   IEnumConnectionPoints ** ppEnum
    );

    HRESULT FindConnectionPoint
    (
        [in]    REFIID riid,
        [out]   IConnectionPoint ** ppCP
    );
}

IConnectionPointContainer demo

  IUnknown *pUnknown;
  HRESULT hr;
  
  CoInitialize(NULL);
  hr =CLSIDFromProgID(OLESTR("InternetExplorer.Application"), &clsid);
  if(hr !=S_OK) {
    return hr;
  }

  hr = CoCreateInstance(clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &pUnknown);
  if(hr !=S_OK) {
    return hr;
  }
  hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (LPVOID *) &pCPC);
  if(hr !=S_OK) {
    return hr;
  }
  IEnumConnectionPoints *pEnum;
  hr = pCPC->EnumConnectionPoints(&pEnum);
  if(hr !=S_OK) {
    return hr;
  }
  
  hr = pCPC->FindConnectionPoint(DIID_DWebBrowserEvents2,&pCP);
  if((hr != S_OK) || (!pCP)) {
    return hr;
  }

IEnumConnectionPoints

IEnumConnectionPoints is a collection or enumerator to obtain connection point details of the server.

  • Next - This method is to get one or more connection point detail in iteration process.
  • Skip - To skip one or more connection in the iteration process.
  • Reset - To rewind the iteration process to the beginning position.
  • Clone - Cloning and creating a new copy of the enumerator object and return to client.
interface IEnumConnectionPoints : IUnknown
{

    [local]
    HRESULT Next(
        [in]                        ULONG               cConnections,
        [out,
         size_is(cConnections),
         length_is(*lpcFetched)]    IConnectionPoint ** rgpcn,
        [out]                       ULONG *             lpcFetched
    );

    HRESULT Skip
    (
        [in]    ULONG   cConnections
    );

    HRESULT Reset
    (
        void
    );

    HRESULT Clone
    (
        [out]   IEnumConnectionPoints **    ppEnum
    );
}

IEnumConnectionPoints demo

  IEnumConnectionPoints *pEnum;
  hr = pCPC->EnumConnectionPoints(&pEnum);
  if(hr !=S_OK) {
    return hr;
  }
  
  if(pEnum){
	ULONG ulConn;
	int conn = 1;
	OLECHAR* guidString;
	printf("InternetExplorer.Application EnumConnectionPoints() returns\r\n");
	do {
	  if(pEnum->Next(1,&pCP,&ulConn) != S_OK) continue;
	  if(pCP) {
        if(pCP->GetConnectionInterface(&iid) != S_OK) continue;
		StringFromCLSID(iid,&guidString);
		wprintf(L"#%d %s [%s]\n",conn, guidString, IID2String(&iid));
	    conn++;
	  }
	}while(ulConn == 1);
  }
  

GUID EventIIDs[3] = {
  {0x9BFBBC02,0xEFF1,0x101A,{0x84,0xED,0x00,0xAA,0x00,0x34,0x1D,0x07}},
  {0xEAB22AC2,0x30C1,0x011CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}},
  {0x34A715A0,0x6587,0x11D0,{0x92,0x4A,0x00,0x20,0xAF,0xC7,0xAC,0x4D}}
};

WCHAR *StrIDs [3] = {
  L"IID_IPropertyNotifySink",
  L"IID_DWebBrowserEvents",
  L"IID_DWebBrowserEvents2"
};
WCHAR * IID2String(GUID *iid)
{
  for(int i =0; i < 3; i++)
  {
    if(memcmp(&EventIIDs[i], iid, sizeof(GUID)) == 0){
      return StrIDs[i];
    }
  }
  return NULL;
}

IEnumConnectionPoints demo output

InternetExplorer.Application EnumConnectionPoints() returns
#1 {34A715A0-6587-11D0-924A-0020AFC7AC4D} [IID_DWebBrowserEvents2]
#2 {EAB22AC2-30C1-11CF-A7EB-0000C05BAE0B} [IID_DWebBrowserEvents]
#3 {9BFBBC02-EFF1-101A-84ED-00AA00341D07} [IID_IPropertyNotifySink]

IConnectionPoint

IConnectionPoint is the interface to deal with individual connection points of the COM server.

  • GetConnectionInterface - returns the GUID/IID of the interface.
  • GetConnectionPointContainer - returns the connection point container object pointer.
  • Advise - Connect one sink object to the server.
  • Unadvise - Disconnect the same sink object connected via Advise.
  • EnumConnections - Returns the enumerator to obtain active connection details.
interface IConnectionPoint : IUnknown
{

    HRESULT GetConnectionInterface
    (
        [out]           IID * piid
    );

    HRESULT GetConnectionPointContainer
    (
        [out]           IConnectionPointContainer ** ppCPC
    );

    HRESULT Advise
    (
        [in]    IUnknown * pUnkSink,
        [out]   DWORD *    pdwCookie
    );

    HRESULT Unadvise
    (
        [in]    DWORD dwCookie
    );

    HRESULT EnumConnections
    (
        [out]   IEnumConnections ** ppEnum
    );
}

IConnectionPoint demo

hr = pCPC->FindConnectionPoint(DIID_DWebBrowserEvents2,&pCP);
if((hr != S_OK) || (!pCP)) {
  return hr;
}
pCP->Advise(&Events,&dwC);

/*Invoke methods here*/
/*Events will be triggered*/

pCP->Unadvise(dwC); /* at end */

IEnumConnections

IEnumConnections is a collection or enumerator to obtain active connection details of the server.

  • Next - This method is to get one or more connection detail in iteration process.
  • Skip - To skip one or more connection in the iteration process.
  • Reset - To rewind the iteration process to the beginning position.
  • Clone - Cloning and creating a new copy of the enumerator object and return to client.
typedef struct tagCONNECTDATA
{
  IUnknown *  pUnk;
  DWORD       dwCookie;
} CONNECTDATA;

interface IEnumConnections : IUnknown
{
  HRESULT Next([in] ULONG           cConnections,
        [out,
         size_is(cConnections),
         length_is(*lpcFetched)]    CONNECTDATA *   rgcd,
        [out]                       ULONG *         lpcFetched
    );

  HRESULT Skip
  (
      [in]    ULONG cConnections
  );

  HRESULT Reset
  (
        void
  );

  HRESULT Clone
  (
      [out]   IEnumConnections ** ppEnum
  );
}

IEnumConnections demo

IEnumConnections *pEnumConn;
pCP->EnumConnections(&pEnumConn);

if(pEnumConn)
{
  CONNECTDATA conndata;
  ULONG ulConn;
  int conn = 1;
  do {
    if(pEnumConn->Next(1,&conndata,&ulConn) != S_OK) continue;
      wprintf(L"#%d IUnknown =%p dwCookie=%d\n",conn, conndata.pUnk, conndata.dwCookie);
      conn++;
  }while(ulConn == 1);
}

IEnumConnections demo output

#1 IUnknown =0265351C dwCookie=1
#2 IUnknown =00425988 dwCookie=2

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#