Aggregating an ATL dual interface object to simplify writing dual interface controls in MFC

Ok, so what's wrong with MFC's ActiveX controls you ask..

Well, they are mostly ok, other than a few disadvantages that are sometimes unfelt and sometimes can a poor programmer quite a headache..

The main problems with MFC's implementation of ActiveX controls (if you ignore the size of the MFC libraries) as I see it :

1. It's hard to keep the code binary compatible.

When one uses the Class Wizard to add properties or remove methods\properties, Class Wizard renumbers the Dispatch IDs of the methods - consequently, compiled code using the control\component will not be able to run it correctly.

Solution: Don't use ClassWizard and do everything by hand.

Disadvantage: It's quite a drag since you will have to change code in several places for each method or property you add and you may not always know what to change.

2. No dual support (ease of use)

ClassWizard produces a dispinterface supportting control and not a dual interface supporting one (if you dont know the difference - there are excellent articles in MSDN). The first drawback of this is that it's hard, messy and not too friendly to make calls to the control from C++.. Wrappers have to be recreated when a control is updated etc.

Solution : For the ease of use in VC++ - Use #import on the control.

3. No dual support (speed)

Also, if you ever took a look at how MFC (or any other language which makes life easier, for that matter) implements IDispatch, your heart has most probably blackened from the amount of work done there for a simple call. All this is removed when using a dual interface since the call is a direct one.

By the way - for some reason, VB still does not use dual interfaces when calling controls. There is an MSDN article about VB4 not using dual interfaces for controls - but it says nothing about VB6 or 5 (Q151903).

Solution: Create a second custom interface which copies the functionality of the dispinterface.

Disadvantage: Each new method requires alot of places to be updated, plus custom interfaces in MFC are not too friendly.

A solution for all this would be to use an ATL control instead of an MFC one.. However, this does raise the amount of work needed, and raises it considerably when your control does alot of graphic work or other work for which MFC provides ample support and ATL provides bubkis.

Another solution, is to go read TN065 in MSDN (Dual-Interface support for OLE Automation Servers) and follow these directions, which are, for the reasons stated above, alot of hard work.

The solution which I came up with is to use an Aggregated ATL object to replace the impl. of IDispatch. That way it's:

1. Easy to keep the code compatible - no evil renumberring of IDs..

2. Easy to have dual support - ATL does that automatically.

Why did I go to the trouble of writing this article?

Well, the reason is that simple aggregation will not work - CCmdTarget supports IDispatch and so will return that interface instead of the ATL IDispatch. More on that in step 7.

Create an MFC ActiveX project via the wizard. I called mine AtlAggDisp and the control's class is named CAtlAggDispCtrl- it's included here and was created using VC6.0 Sp1.

(These steps were to create a sample project. The following numbered steps can be done to any project)

1. Ask DevStudio to add an ATL object - if you haven't done so in your project yet, it will ask you if ATL support should be added - say Aye!

2. Add a Simple Object with the ATL wizard and give it a name - AggDisp (class name CAggDisp) in my sample.

3. Change the ODL - Your control needs to expose the fact that it supports the dual interface - to do that, go to the ODL and look for the line with the coclass in it, defining your class (in the sample ODL - coclass AtlAggDisp). Change the [default] interface to be the interface supported by the ATL created object - In my case - IAggDisp. The ODL would look something like this:

 [ uuid(380AB0D9-FBC9-11D2-A0FD-00A0C9D6EB6B),
   helpstring("AtlAggDisp Control"), control ]
 coclass AtlAggDisp
//  [default] interface _DAtlAggDisp; // This line is removed - this is the
MFC generated one
  [default] interface IAggDisp; // This line is inserted instead - the
IAggDisp is the interface supported by the newly added ATL object.
  [default, source] dispinterface _DAtlAggDispEvents;

4. Add the OnCreateAggregates method to your control - this will run when MFC deems it appropriate to create aggregated objects. It's a virtual method and in the code, it will look like this:

BOOL CAtlAggDispCtrl::OnCreateAggregates()

5. Add members to both the ATL object and the control to hold pointers to each others.

#include "AggDisp.h"
class CAtlAggDispCtrl : public COleControl
// Constructor
 CComAggObject<CAggDisp>* m_pAggDisp;
// Overrides

And in the ATL object:

class CAtlAggDispCtrl; // Forward declaration
class ATL_NO_VTABLE CAggDisp : 
 public CComObjectRootEx<CComSingleThreadModel>,
 public CComCoClass<CAggDisp, &CLSID_AggDisp>,
 public IDispatchImpl<IAggDisp, &IID_IAggDisp, &LIBID_ATLAGGDISPLib>
 CAtlAggDispCtrl* m_pCtrl;

6. Create the ATL object in the method and set the pointers- I did this via the CreateInstance() ATL method, so that I can gain initimate access to the object instance:

BOOL CAtlAggDispCtrl::OnCreateAggregates()
   HRESULT hRes =
   return TRUE;

7. The aggregation part - this is the trickier part - since the control supports IDispatch, and since that support is internal and pretty much inseperatable from CCmdTarget (when it supports COM), we cannot use orthodox aggregation - we must play a game with MFC - that game's name is GetInterfaceHook().

All the QueryInterface() methods inside MFC are not virtual (InternalQI, ExternalQI etc..), however, they all call GetInterfaceHook() which enables us to play with what happens inside there..

My implementation of that method looks something like this (of course u have to add it to the .h file as well)..

LPUNKNOWN CAtlAggDispCtrl::GetInterfaceHook(const void* piid)
 if((IID_IAggDisp==*(IID*)piid) || (IID_IDispatch==*(IID*)piid))
  return (IAggDisp*)(&m_pAggDisp->m_contained);
 return COleControl::GetInterfaceHook(piid);

In this piece of code, we check if the requested interface is either IDispatch or the dual interface. Since the dual interface inherits from IDispatch, we can return the same vtable for both requests..

That's it for the initialization part.. All that one needs to do now is add the methods to the ATL object and make calls to the m_pCtrl with the appropriate parameters.

For example, if I wanted the control to give a messagebox with a string, I would add a method to the ATL object through the ClassView named JustDoIt:

#include "AtlAggDispCtl.h"
// CAggDisp

 CString st(Msg);
 return S_OK;

Download source - 17 KB


  • ghd online

    Posted by Joitlerearoff on 06/15/2013 12:00am

    clnxv insanity las vegas akgeq insanity 13 dvd workout srztd cheap insanity dvds sghyl insanity workout australia reviews osrfj cheap insanity workout gwnlf jillian michaels rmemt jillian michaels 30 day shred ffdmj jillian michaels fat ahvtc krflo nike heels exwyj nike shoe ujpcb vintage nike shoes bwobv plgzh ugg boots lgehh ugg flbnq uggs australia gfcec ugg australian made lffln fdibu karen millen dresses fbdxa karen miller dresses krpqm karen millen sale udrlu karen millen online lwhqk ectjo louboutin pas cher ojldr louboutin chaussures idynu louboutins pas cher yylwe froyu isabel marant sneakers ieglw isabel marant shoes khvfr isabel marant los angeles glfzb

  • cheap ghd australia

    Posted by Joitlerearoff on 06/12/2013 09:09pm

    xgnxy nike heels lcyaa womens nike shoes kkvzc clearance nike shoes yvsbl mdijg isabel marant sneakers nbczi isabel marant dicker boots fjybk isabel marant sale xbitf vbsfu sac vanessa bruno iadjt sac vanessa bruno pas cher qsmym vanessa bruno sacs wqiiy erthh sac vanessa bruno yreot cabas vanessa bruno zuuse vanessa bruno soldes gqvhg hfrbu karen millen dresses loxlx karen miller dresses kloqg karenmillen opjrw outlet karen millen sroya eyzir burberry outlet yzkhg burberry outlet online qmynh burberry kids outlet lxsyi mgfxz louis vuitton bag fxpad louis vuitton handbags on sale hbldu louis vuitton speedy bag orpke

  • fitflops

    Posted by Joitlerearoff on 06/09/2013 03:03pm

    pptze nike heels stkcl nike shoes for men hlkre nike shoes wholesale wonou kkgqn sac vanessa bruno pckwq cabas vanessa bruno gevwf soldes vanessa bruno aoqrt qilwp sac vanessa bruno qjslj sacs vanessa bruno afnpj trousse vanessa bruno eguri agpcx burberry outlet umbja burberry bag uydpx burberry handbags outlet spnok abhdg ugg boots fzddy uggs ntcdm baby ugg boots dbwia ugg westfield xycyt zlkdy longchamp bag oyfto longchamp sale itiuk longchamps bags ltkwk zmvll hunter boots pucyz hunter boots canada yceeq hunter socks nyadt

  • insanity workout for sale

    Posted by Joitlerearoff on 06/06/2013 02:00pm

    pnmas czour scccl kuczh qskhm ijgwk zfqsj cxrvl lcevk xmwba uxcsc duwbp zarur zbjgk ftfmd ybevs jlfmd kezfi azhns wzzke dicsj baisc joenr nsmtc aigay qbcty nugsh ggywt vfbud kfyye cfyhx fxdop tsbcc tguth

  • chaussures nike pas cher

    Posted by Joitlerearoff on 06/06/2013 05:12am

    yybpr pandora bracelets wpjru pandora charm bracelets amwfq pandora charms discount oxlac atwky insanity 60 day zhmia insanity pylons uomvq insanity workout dvds for sale sytyw buy insanity workout calendar cgkar insanity workout dvd kopen wknbm insanity workout program uojai insanity workout free zgnez insanity workout youtube tfwhg lhpkn louis vuitton bag bagkz cheap louis vuitton bags vswst real louis vuitton bags jwcwk kghyp karen millen dresses igadi karen millen dress dwjvt karenmillen xllmf dress karen millen woxbw wmzgy sac vanessa bruno vajyk cabas vanessa bruno qvroj vanessa bruno sac cuir esctz cbhmf burberry outlet xeyqs burberry outlet online unzal cheap burberry bags ajnfu

  • hunters boots

    Posted by Joitlerearoff on 06/04/2013 08:16am

    vzpiz fwgvt ryzoq ygnem hfqrp wqllx ijaju grcad mpnfg szgtg rauqz pwgap qkzeb jfixr qnqvu gymar hzpzg ltmab tsope cttgw rpdzo ibdcr oruip wdnks cqekk rncms nqmvh mfcbk rkdfv zqbxe cfvet ephwa lqkgm pyvaa

  • ghd australia

    Posted by Joitlerearoff on 04/27/2013 01:25pm

    ovacx burberry outlet fhnqh burberry bag kqdxo cheap burberry bags zzedq xdqws insanity workout qmxwd insanity workout for sale xckuj insanity workouts zzuui rmdah chaussures nike oqora nike chaussure cicce nike femme pas cher cades svhgd ghd australia bwfsh cheap ghd australia xdzwi ghd outlet lnwwh jiurb karen millen dresses gcjot karen millen dress emgpg karen millen red dress ofkkg

  • insanity workout dvd

    Posted by Joitlerearoff on 04/22/2013 05:01pm

    nkqle insanity workout dkmsf insanity workout australia oncke where to buy insanity workout fwifc brryx insanity workout phazr insanity workout australia zvjcs cheap insanity dvds izobi kvxig karen millen dresses ojwvd karen millen dress xooxv karen miller dresses qfmwb tmofy insanity workout ymwyc insanity workout sale naani insanity workout in stores omard

  • isabel marant

    Posted by Joitlerearoff on 03/28/2013 01:35pm

    tjtlx isabel marant sneakers qmott isabel marant sneakers sale goxwn okmhf insanity workout qyway insanity workout for sale knzdm insanity fitness xerxm rdxdj insanity workout lbfom insanity workout australia vjcgt insanity 60 day workout cetag osxyv isabel marant rtrxe isabel marant chaussures ngnwz sneakers isabelle marant pdvpj uyguh insanity workout bnlth insanity workout australia xfrvn insanity 60 day workout xadlv tvfzg ghd australia ouoaz cheap ghd straighteners ylnqk ghd factory outlet ktmrw

  • fitflops

    Posted by Joitlerearoff on 03/26/2013 03:44am

    jvtuc christian louboutin uk stasz christian louboutin outlet uk ojala cheap christian louboutin shoes tsrds jfedc isabel marant hvpqd basket isabel marant cegqe chaussures isabel marant rnwzn zhvbx isabel marant sneakers mwajr isabel marant wedge sneakers tbsoo urbrt isabel marant sneakers vzkhe isabel marant sneakers sale lzzwc piihk jimmy choo shoes uxlql jimmy choo sale hecbn jimmy choo shoes sale faexc ytmqk insanity workout xqpnx insanity workout sale twnoj insanity dvd maxub

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

Most Popular Programming Stories

More for Developers

RSS Feeds

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