COMEvents Sample: Demonstrates Creating and Using COM Events

The COMEvents sample demonstrates event handling using COM.

Security noteSecurity 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. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

To build and run COMEvents

  1. Open the solution file COMEvents.sln.

  2. From the Build menu, click Build Solution.

  3. Right-click the EventReceiver node in Solution Explorer and select Set as Startup Project.

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

How the Sample Works

The COMEvents sample creates an event source CSource and an event receiver CSink using the event_source and event_receiver attributes, respectively.

The event source declares the methods in the interfaces IEvent, IEvent2, and IJW as events (see Ifaces.h for the interface definitions).

The event receiver declares handler methods f1 through f6, and hooks each of these handlers to an event.

The main code instantiates CSource and CSink. The CSink constructor performs the hooking. The main code then fires the events, unhooks the events, then fires the events again.

See Also

Other Resources

Event Handling Samples