ATL Event Handling Summary

Event Handling and ATL

In general, handling COM events is a relatively simple process. There are three main steps:

Implementing the Interface

There are four main ways of implementing an interface using ATL:

Derive from Suitable for Interface type Requires you to
implement all methods
*
Requires a type
library at run time
The interface Vtable Yes No
IDispatchImpl Dual Yes Yes
IDispEventImpl Dispinterface No Yes
IDispEventSimpleImpl Dispinterface No No

* When using ATL support classes, you are never required to implement the IUnknown or IDispatch methods manually.

Advising and Unadvising the Event Source

There are three main ways of advising and unadvising an event source using ATL.

Advise function Unadvise function Most suitable for use with Requires you to keep
track of a cookie?
Comments
AtlAdvise, CComPtr::Advise, or CComQIPtr::Advise AtlUnadvise Vtable or dual interfaces Yes AtlAdvise is a global ATL function; CComPtr::Advise and CComQIPtr::Advise just wrap calls to this function when the contained pointer is to an interface on the event source object.
IDispEventSimpleImpl:
:DispEventAdvise
IDispEventSimpleImpl:
:DispEventUnadvise
IDispEventImpl or IDispEventSimpleImpl No Fewer parameters than AtlAdvise since the base class does more work.
CComCompositeControl:
:AdviseSinkMap
CComCompositeControl:
:AdviseSinkMap
Composite controls derived from IDispEventImpl No CComCompositeControl::AdviseSinkMap advises all the entries in the event sink map. The same function unadvises the entries. This method is called automatically by the CComCompositeControl class.

IDispEventImpl, IDispEventSimpleImpl, Supporting IDispEventImpl

See Also

AtlAdvise, AtlUnadvise, CComCompositeControl::AdviseSinkMap, CComPtr::Advise, CComQIPtr::Advise, IDispEventSimpleImpl::DispEventAdvise, IDispEventSimpleImpl::DispEventUnadvise