ATLDuck Attributes Sample: Uses Connection Points with ATL

The ATLDuck attributes sample consists of two projects: atlduck and duck. The duck project creates an instance of an object that implements the IDuckInt interface. This interface includes four member functions: Flap, Paddle, Quack, and Walk.

The second project, atlduck, has a connection point for the IDuckInt interface (that is, it knows how to use the interface but does not implement it). Only a single instance of this object will be created. Once the connection is established between the two applications, atlduck will call the functions in the IDuckInt interface for sinks that have called IConnectionPoint::Advise.

Various IDL, COM, and compiler attributes are demonstrated in ATLDuck, including aggregatable, event_source, and event_receiver. The DuckInt class is made unaggregratable by specifying the aggregratable("never") attribute. The DuckDoer class is set up as a com event_source, and the DuckInt class as a com event_receiver, without layout dependent=true.

The ATLDuck sample is the nonattributed version of this sample.

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 this sample

  1. Open the solution file atlduck.sln.

  2. From the Build menu, click Build Solution. This will build both projects and perform the necessary registration. The following files will be created:

    • atlduck.exe, an EXE server

    • duck.exe, an EXE client

To run this sample

  1. From the Debug menu, click Start Without Debugging. This will start the atlduck server and one instance of the duck client. To run more than one client, navigate to the directory where duck.exe is located and run it manually from the command prompt.

  2. A dialog box appears. In the dialog box, click the button Create DoDuck Object. When you click this button, the application creates an instance of an object with class ID CLSID_DuckDoer, running atlduck.

  3. A new dialog box, issued by atlduck.exe, appears. This dialog box shows a button for each function in the IDuckInt interface, as well as a list box with the connections to active sinks and their cookies. From the duck dialog boxes, you can either Advise or Unadvise the connection point. Depending on your choice, you will receive or not receive notifications from the sources. The notification, when you receive it, will show in the status edit field. Additionally, ATLDuck demonstrates the value of the cookie supplied by the connection point when the connection is advised.

Attributes

This sample uses the following attributes:

  • ATLDuck   aggregatable, coclass, dispinterface, event_source, exe, helpstring, id, module, name, progid, uuid, vi_progid

  • ATLDuck/Duck   aggregatable, coclass, default, dispinterface, event_receiver, exe, helpstring, id, iid_is, in, module, name, object, out, progid, unique, uuid, version, vi_progid

Classes and Keywords

The sample use the following classes:

CDialog (MFC), CComObject (ATL), IConnectionPointImpl<CDuckDoer> (ATL)

This sample uses the following keywords:

_ASSERTE; _tcslen; _VERIFY; AddRef; Advise; BEGIN_COM_MAP; BEGIN_CONNECTION_POINT_MAP; BEGIN_MSG_MAP; BEGIN_OBJECT_MAP; CComCoClass; CComModule::Unlock; CComObjectRootEx; CDialogImpl; CenterWindow; CoInitialize; COM_INTERFACE_ENTRY; COM_INTERFACE_ENTRY_IMPL; COMMAND_HANDLER; COMMAND_ID_HANDLER; CONNECTION_POINT_ENTRY; CoSuspendClassObjects; CoUninitialize; Create; DECLARE_CLASSFACTORY_SINGLETON; DECLARE_NOT_AGGREGATABLE; DECLARE_REGISTRY_RESOURCEID; DestroyWindow; DispatchMessage; EnableWindow; END_COM_MAP; END_CONNECTION_POINT_MAP; END_MSG_MAP; END_OBJECT_MAP; ExitProcess; FindConnectionPoint; GetCommandLine; GetDC; GetDlgItem; GetMessage; GetTextExtentPoint32; IConnectionPointContainerImpl; IConnectionPointImpl; Init; IsWindowVisible; MESSAGE_HANDLER; MessageBox; OBJECT_ENTRY; OleInitializeCoCreateInstance; OnCancel; OnFlap; OnInitDialog; OnOK; OnPaddle; OnWalk OnQuack; PostThreadMessage; QueryInterface; RecalcListboxExtent; RegisterClassObjects; RegisterServer; reinterpret_cast; ReleaseDC; RevokeClassObjects; SendMessage; SetOwner; SetWindowText; ShowStatus; ShowWindow; Unadvise; UnregisterServer; UNUSED_ALWAYS; UpdateRegistryFromResource; UpdateWindow; USES_CONVERSION

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