CONNECT: Demonstrates Implementation and Use of Connection Points

Click to open or copy the CONNECT project files.

This sample illustrates the implementation and use of connection points (the IConnectionPointContainer and IConnectionPoint interfaces) in a multithreaded environment.

The server is implemented in the Connect.dll. This DLL allows the creation of a CoRandom COM object, implemented by the CRandom C++ class. The COM object supports IRandom (a dual interface) and IConnectionPointContainer, and it accepts connections for the IRandomEvent interface.

The IRandom interface supports the following methods:

When running, the secondary threads inside the object keep firing events through the connection point.

Two clients are provided: DRIVE and MDRIVE. They can be found in the DRIVE and MDRIVE subdirectories.

Drive.exe is a simple console application that provides a single object implementing the IRandomEvent interface. It creates a CoRandom object, calls Advise/Unadvise on the connection point, and makes the CoRandom object fire events into the drive's object.

Mdrive.exe is an MFC dialog-based application can create multiple advise sinks and control the number of threads the server creates. When you run Mdrive.exe, click the Start button at least once, then click the Advise button several times. Each click of the Advise button adds a connection point, which makes the display wider. If you do not click the Advise button, you will see no activity in the display.

Running the Sample

Build Connect.dll and register it. You can build and run Drive.exe from the command line, or build and run Mdrive.exe from Visual C++.

The CONNECT sample will not register correctly when its path contains spaces. One solution to this problem is to edit the corresponding .rgs file and add single quotes around all occurrences of %MODULE%. For example, you would change a line containing the following:

InprocServer32 = s %MODULE%

to the following:

InprocServer32 = s '%MODULE%'

This sample uses the following keywords:

AfxGetApp; AfxMessageBox; AtlAdvise; ATLASSERT; AtlUnadvise; BEGIN_COM_MAP; BEGIN_CONNECTION_POINT_MAP; BEGIN_MESSAGE_MAP; BEGIN_OBJECT_MAP; CComCoClass; CComModule::GetClassObject; CComModule::GetLockCount; CComModule::Init; CComModule::RegisterServer; CComModule::Term; CComModule::UnregisterServer; CComObject::CreateInstance; CComObjectRoot; CDialog::OnCancel; CDialog::OnOK; CloseHandle; CoCreateInstance; COleTemplateServer::RegisterAll; COM_INTERFACE_ENTRY; COM_INTERFACE_ENTRY_IMPL; COM_INTERFACE_ENTRY2; CONNECTION_POINT_ENTRY; CoUninitialize; CreateEvent; CreateThread; DECLARE_REGISTRY_RESOURCEID; DisableThreadLibraryCalls; DoModal; DrawIcon; Enable3dControls; Enable3dControlsStatic; END_COM_MAP; END_CONNECTION_POINT_MAP; END_MESSAGE_MAP; END_OBJECT_MAP; GdiFlush; GetClientRect; GetDlgItem; GetTickCount; GetUnknown; IConnectionPointContainerImpl; IConnectionPointImpl; IDispatchImpl; ISupportErrorInfo; LoadIcon; Lock; memset; OBJECT_ENTRY; ON_COMMAND; puts; ReleaseDC; SendMessage; SetEvent; SetIcon; SetPixel; Sleep; Unadvise; Unlock; WaitForSingleObject