# CONNECT Sample: Demonstrates Implementation and Use of Connection Points (C++ Samples)

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

Note
This sample is a modification of the ATL sample Connect to demonstrate compiler COM support for connection point sinks. The Drive client is a native implementation, whereas the MDrive client slightly belabors COM-correctness in using a class factory to create sink objects. Neither Drive nor MDrive depend on ATL or MFC for any COM support.

Security Note
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices.

# Building and Running the Sample

Note
If you are running Visual C++ Express Edition, you might need to install the Platform SDK before running this sample. For information on how to do this, see How to: Use Visual C++ Express Edition with the Microsoft Platform SDK.

#### To build and run this sample

1. Open the solution connect.sln.

2. On the Build menu, click Build Solution.

3. Select which client you want to run, Drive or MDrive, and make it the startup project (right-click the project folder and click Set as StartUp Project). More information about the clients can be found in the How the Sample Works section.

4. On the Debug menu, click Start Without Debugging.

# How the Sample Works

The server is implemented in 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.

- `Start` Starts a thread inside the object.

- `Stop` Stops a thread inside the object.

- `StopAll` Stops all running threads.

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` and `Unadvise` on the connection point, and has the `CoRandom` object fire events into the drive's object.

- Mdrive.exe is an MFC dialog-based application, able to create multiple advise sinks and to 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 not see any activity in the display.