Multiple Dispatch Interfaces in ATL

Most of COM objects implement more than one interface. These interfaces are usually dual interfaces, which means they implement IDispatch as well. If you use such an object from a programming environment that can access IUnknown, all the interfaces are available through the QueryInterface method.  Using the object from scripting (automation) environments (like VBScript) leave you with only one interface - IDispatch.

Consider the following example:

Let's say that there are two interfaces, IFly and IWalk, that you want to implement in your object Bird. Both interfaces are defined as dual, so each of them has an IDispatch interface. Your object Bird implements the interfaces, but also have to expose IDispatch interface in order to allow it to be accessed by an automation environment. Since only one IDispatch is allowed (using QueryInterface with IDispatch should always returns the same interface), you have couple of  options:

  • Chose one of the interfaces (IFly or IWalk) and use its IDispatch as the IDispatch exposed by Bird. This is the default option for an ATL object. If you have more than one interface derived from IDispatch, you have to specify each one should be used for IDispatch with the macro COM_INTERFACE_ENTRY2(IDispatch, yourInterface). This approach leaves parts of your object inaccessible from automation.
  • Design a new IDispatch that implements all the functionality from both interfaces. Drawback: adds a lot of complexity ( multiple interfaces to be delegated, big number of properties and methods) and more work for each object( you have to design new interfaces).
  • Create methods in your interfaces that give access to other interfaces. That reduces the reusability of your interfaces. It also violates the symmetric and transitive principles, since the methods will return interfaces to the same object.

The Solution

The goal is to find a legitimate way to identify different IDispatch implementations and ability to access them from automation environments.
The solutions is to add a new method to each IDispatch (but not to the interface itself), called QueryDispatch. It will provide navigation between the different IDispatches. The implementation should act like QueryInterface ; i.e. be reflexive, symmetric, and transitive.

Property Meaning
Reflexive pIOne->QueryInterface(IID_IOne) must succeed.


Symmetric If pITwo was obtained from pIOne->QueryInterface(IID_ITwo), then pITwo->QueryInterface(IID_IOne) must also succeed.


Transitive If pITwo was obtained from pIOne->QueryInterface(IID_ITwo) and pIThree was obtained from pITwo->QueryInterface(IID_IThree), then pIThree->QueryInterface(IID_IOne) must also succeed.

The IDL definition is:

[propget, id(IDQUERYDISPATCH)]
HRESULT QueryDispatch([in] BSTR dispName,[out, retval] VARIANT* pVal);

In ATL  the dispatch part of a dual interface is implemented automatically. All you have to do is provide access to it. This is done by modifying the IDispatchImpl. The new method (QueryDispatch) is implemented by default.

The Implementation

The system files that you need are:

QueryDispatch.h - header file with macros for ATL

Implement your dual interfaces as usual. Substitute IDispatchImpl with IDQDispatchImpl,  add BEGIN_QUERYDISPATCH_MAP and you are done.

#include " QueryDispatch"
class ATL_NO_VTABLE CQryDispObj :
	   public CComObjectRootEx<CComSingleThreadModel>,
	   public CComCoClass<CQryDispObj, &CLSID_QryDispObj>,
	   public IQDDispatchImpl<CQryDispObj,  IQryDispObj,  &IID_IQryDispObj,  &LIBID_QUERYDISPATCHTESTLib>,
	   public IQDDispatchImpl<CQryDispObj,  IWalk,   &IID_IWalk,   &LIBID_QUERYDISPATCHTESTLib>

    // Specify that IQDDispatchImpl interface will be used    as default IDispatch for the object 
    // Declare all dual interfaces that will be   supported. The names are the name of the interface (without the IID_ part). 

The Scripting Environment

For each object which implements multiple IDispatches and each IDispatch must include QueryDispatch. To use any dual interface of the object, you need to obtain the IDispatch for the object , query for the desired interface and use it as usual. Here is example for VBScript:

	'create the object as usual
	Dim server 
	Set server = CreateObject("vp.QryDispObj")
	'query for the IWalk dispatch interface

Dim dispWalk set dispWalk = server.QueryDispatch("IWalk") 'use the interface as usual dispWalk.Walk = "walk"

Download demo project - 11 KB

Download source - 3 KB



  • Thanks a lot for this solution :) But a Bug live in the code

    Posted by Tangi MAURY on 02/12/2016 03:33am

    for (int j=m_nCount-1; j=0; j--){ = int j=m_nCount-1; for (; j=0; j--){ Because j is used after the loop

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • For many organizations, moving enterprise applications to the public cloud can be a very attractive proposition, but planning the best way to move your applications is mission–critical. As an alternative to the costly option of re–architecting the application for a cloud environment, you can follow a "lift and shift" model that's significantly cheaper and almost always a lot quicker. In order to have a successful "lift and shift" migration, read this white paper to learn a few rules you should …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date