DispSink Attributes Sample: Handles Events Fired from a Singleton COM Server Through a Dispatch Interface

The DispSink attributes sample demonstrates a singleton server object (an object that can have only one instance) that has its own dual interface and a dispatch interface used for firing off events.

The DispSink sample is the nonattributed version of this sample.

Note

This sample may not exactly demonstrate some features of the Visual C++ wizards, libraries, and compiler, but it still shows how to complete the intended task.

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 DispSink.sln.

  2. From the Build menu, click Build Solution.

  3. Open two or more instances of the ActiveX Control Test Container and insert the client control, DispCtl, into each instance. See Testing Properties and Events with Test Container for information on how to access the test container.

  4. Invoke the Connectmethod on all of the controls.

  5. Invoke the Send method on one of the controls. Change the Parameter Type field of the Invoke Methods dialog box to VT_BSTR and then type any string into the Parameter Value box. Click the Invoke button. The string will be displayed in the center of all connected controls.

  6. Invoke the Disconnect method on all controls prior to deleting them.

How the Sample Works

The server is a singleton object that has its own dual interface as well as a dispatch interface used for firing off events. The dispatch interface is placed in the .idl file using the dispinterface attribute. The server object receives data through its dual interface Send method and transmits it to all connected components through the Transfer event on its dispatch interface. The Dispserver uses the event_source attribute, and the Dispclient uses the event_receiver attribute.

The client is an ActiveX control that contains a server object. The control responds to the Transfer event fired by the server object. It has a dual interface that has Connect, Send, and Disconnect methods. If the Transfer event is fired with a variant containing a BSTR, the string is displayed in the center of the control.

Attributes

This sample uses the following attributes:

  • DISPSINK/DispClient   coclass, dual, event_receiver, helpstring, id, module, object, pointer_default, registration_script, threading, uuid, version, vi_progid

  • DISPSINK/DispServer   coclass, default, dispinterface, dual, event_source, helpstring, id, module, object, pointer_default, threading, uuid, vi_progid

Keywords

This sample uses the following keywords:

#import; CComAutoThreadModule; CComSimpleThreadAllocator; CoCreateInstance; DECLARE_CLASSFACTORY_AUTO_THREAD; FireViewChange

See Also

Other Resources

ATL Attributes Samples