Table of contents
1.
Introduction
2.
ATL and Event Handling
2.1.
Event Handling Principles
3.
Implementing the event handling interface
3.1.
IDispatchImpl Class
3.1.1.
Syntax
3.2.
IDispEventImpl Class
3.2.1.
Syntax
3.3.
IDispEventSimpleImpl Class
3.3.1.
Syntax
3.4.
Summary
4.
Frequently Asked Questions
4.1.
What is ATL C++?
4.2.
How do I create an ATL COM object in Visual C++?
4.3.
What are events?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Implementing the event handling interface

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Reader!!

As we know, Microsoft's Active Template Library is a collection of template-based C++ classes designed to make writing Component Object Model objects easier. To make this point even clearer, we will start with our implementation and learn to implement the event handling interface in ATL. 

 Event handling interface in ATL

So, let’s get started!

ATL and Event Handling

The Active Template Library (ATL) is a collection of template-based C++ classes that allow you to construct compact, efficient COM objects. It supports significant COM features such as stock implementations, dual interfaces, standard COM enumerator interfaces, connection points, tear-off interfaces, and ActiveX controls.

Events inform a client that something interesting has happened in the object, such as a property change or the user clicking a button. Events are very useful with COM controllers. COM objects fire events and no response from the client is required. In other words, they are only alerts. On the other hand, requests are how a COM object asks a query to the client and expects a response.

When we create our standard COM objects, we provide implementations for interfaces (specified in an IDL file) that we have written or given to us. Such implementations make it easier to create "incoming" interfaces. The term "incoming" implies that the object "listens" to its client. The client invokes interface methods and so "talks" to the object. Let us learn about these implementations of event-handling interfaces in ATL.

Event Handling Principles

Every event-handling procedure follows these three phases. We must:

  • Add the event interface to your object.
  • Inform the event source that your object would want to accept events.
  • When your object no longer requires events, notify the event source.


The type of event interface will determine how we implement it. A vtable, dual, or dispinterface event interface can exist. It is up to the event source's designer to specify the interface; it is up to us to implement that interface.

Implementing the event handling interface

ATL can support you with all three components of event handling: implementing the event interface, advising the event source, and unadvising the event source. The type of event interface and the performance needs of your application determine the specific actions you must take.

The following are the most typical ATL interface implementation methods:

  • Directly derived from a custom interface.
  • Derived from IDispatchImpl for dual interfaces defined in a type library.
  • Derived from IDispEventImpl for dispinterfaces defined in a type library.
  • Derived from IDispEventSimpleImpl for dispinterfaces not specified in a type library or to enhance efficiency by not loading type information at runtime.

Let us now see each of the above classes briefly.

IDispatchImpl Class

IDispatchImpl is the default implementation for the IDispatch component of any dual interface on an object. A dual interface is based on IDispatch and only supports Automation-compatible types. A dual interface, like a dispinterface, offers early and late binding, but it also enables vtable binding.

Syntax

template<class Temp,
        const IID* tiid= &__uuidof(Temp),
        const GUID*t libid = &CAtlModule::m_libid,
        WORD libMajor = 1,
        WORD libMinor = 0,
        class tihclass = CComTypeInfoHolder>
class ATL_NO_VTABLE IDispatchImpl : public Temp

 

Parameters

  • Temp: A dual interface.
  • tiid: A pointer to the IID of Temp.
  • tlibid: A pointer to the type library's LIBID, which includes information about the interface. The server-level type library is given by default.
  • libMajor: The type library's major version. The value is set to 1 by default.
  • libMinor: The type library's minor version. The value is set to 0 by default.
  • tihclass: Temp's type information is managed by this class. CComTypeInfoHolder is the default value.

IDispEventImpl Class

IDispEventImpl allows you to implement an event dispinterface without having to give an implementation code for each method/event on that interface. IDispEventImpl offers IDispatch method implementations. You just need to provide implementations for the events that you want to handle.

Syntax

template <UINT tID, class Temp,
    const IID* tdiid = &IID_NULL,
    const GUID* tlibid = &GUID_NULL,
    WORD libMajor = 0,
    WORD libMinor = 0,
    class tihclass = CcomTypeInfoHolder>
class ATL_NO_VTABLE IDispEventImpl : public IDispEventSimpleImpl<tID, Temp, tdiid>

 

Parameters

  • tID: The source object's unique identification. When IDispEventImpl is the base class for a composite control, this argument should be set to the resource ID of the desired enclosed control. In all other circumstances, use any positive integer.
  • Temp: The user's class, which is inherited from IDispEventImpl.
  • tdiid: The IID reference for the event dispinterface that is implemented by this class. This interface must be declared in the tlibid, libMajor, and libMinor type libraries above.
  • tlibid:A reference to the type library that defines the dispatch interface indicated by tdiid. If &GUID_NULL is specified, the type library will be loaded from the object that is sourcing the events.
  • libMajor: The type library's major version. The value is set to 1 by default.
  • libMinor: The type library's minor version. The value is set to 0 by default.
  • tihclass: Temp's type information is managed by this class. CComTypeInfoHolder is the default value. You can, however, override this template parameter by giving a class of a different type than CComTypeInfoHolder.

IDispEventSimpleImpl Class

IDispEventSimpleImpl allows you to implement an event dispinterface without having to write implementation code for each method/event on that interface. IDispEventSimpleImpl offers IDispatch method implementations. You just need to provide implementations for the events that you want to handle.

Syntax

template <UINT tID, class Temp, const IID* tdiid>
class ATL_NO_VTABLE IDispEventSimpleImpl : public _IDispEventLocator<tID, tdiid>

 

Parameters

  • tID:  The source object's unique identification. When IDispEventSimpleImpl is the base class for a composite control, this argument should be set to the resource ID of the desired enclosed control. In all other circumstances, use any positive integer.
  • Temp: The class of the user, which is derived from IDispEventSimpleImpl.
  • tdiid:  The event dispinterface IID pointer that this class has implemented.

Summary

If you're creating a custom or dual interface, notify the event source using AtlAdvise or CComPtrBase::Advise. You'll have to keep track of the cookie returned by the call. To terminate, call AtlUnadvise.

If you are using IDispEventImpl or IDispEventSimpleImpl to implement a dispinterface, you should notify the event source by executing IDispEventSimpleImpl::DispEventAdvise. To end the connection, use IDispEventSimpleImpl::DispEventUnadvise.

If you use IDispEventImpl as the base class of a composite control, CComCompositeControl::AdviseSinkMap will automatically advise and unadvise the event sources indicated in the sink map.

Frequently Asked Questions

What is ATL C++?

The Active Template Library (ATL) is a collection of template-based C++ classes that allow you to construct small, fast COM objects.

How do I create an ATL COM object in Visual C++?

First, open the Visual C++ tab. Choose MFC/ATL. In Visual Studio 2019, go to File > New > Project, put "atl" in the search box, and then choose ATL Project.

What are events?

Events are created as a result of user interaction with the graphical user interface components.

Conclusion

In this article, we learned to implement the event-handling interface. We learned about the different ways of implementing the event-handling interface in ATL.

We hope this post has helped you better grasp the ATL event handling interface implementation. You can refer to our blogs to understand more about Interfaces and c++ concepts.


You can also visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can prepare for interviews with interview experiences and an interview bundle.

Keep learning and keep growing, Ninjas!

Thankyou
Live masterclass