CONNECT Attributes Sample: Demonstrates Implementation and Use of Connection Points

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

The sample demonstrates several commonly used IDL, COM, and compiler attributes, including support_errorinfo, event_source, and event_receiver.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run this sample

  1. Open the solution file connect.sln.

  2. From 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 of the 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 makes 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 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.

Attributes

This sample uses the following attributes:

  • Connect   coclass, default, dll, dual, event_source, helpstring, id, in, module, name, object, out, pointer_default, progid, support_error_info, uuid, vi_progid

  • Connect/drive   event_receiver, module

Keywords

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; 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

Note

Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

ATL Attributes Samples