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
{
 DECLARE_DYNCREATE(CAtlAggDispCtrl)
 
// Constructor
public:
 CAtlAggDispCtrl();
 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>
{
public:
 CAggDisp()
 {
  m_pCtrl=NULL;
 }
 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 =
CComAggObject<CAggDisp>::CreateInstance((LPUNKNOWN)GetInterface(&IID_IUnknow
n),&m_pAggDisp);
   m_pAggDisp->m_dwRef=1;
   m_pAggDisp->m_contained.m_pCtrl=this;
 
   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
 

STDMETHODIMP CAggDisp::JustDoIt(BSTR Msg)
{
 CString st(Msg);
 m_pCtrl->MessageBox(st,"Msg");
 return S_OK;
} 

Download source - 17 KB



Comments

  • 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 http://fitness.kachile.org/ krflo nike heels exwyj nike shoe ujpcb vintage nike shoes bwobv http://nikeheel.smchillel.org/ plgzh ugg boots lgehh ugg flbnq uggs australia gfcec ugg australian made lffln http://bootsaustralia.10six.org/ fdibu karen millen dresses fbdxa karen miller dresses krpqm karen millen sale udrlu karen millen online lwhqk http://karenmillendresses.10six.org/ ectjo louboutin pas cher ojldr louboutin chaussures idynu louboutins pas cher yylwe http://louboutinpascher.smchillel.org/ froyu isabel marant sneakers ieglw isabel marant shoes khvfr isabel marant los angeles glfzb http://isabelmarant.10six.org/

    Reply
  • cheap ghd australia

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

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

    Reply
  • fitflops

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

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

    Reply
  • insanity workout for sale

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

    pnmas http://insanedvd.area8sova.org/insanityworkoutonline2.asp czour http://insanedvd.area8sova.org/insanityworkoutonline4.asp scccl http://insanedvd.area8sova.org/insanityworkoutonline6.asp kuczh http://insanedvd.area8sova.org/insanityworkoutonline11.asp qskhm http://insanedvd.area8sova.org/insanityworkoutonline14.asp ijgwk http://insanedvd.area8sova.org/insanityworkoutonline15.asp zfqsj http://insanedvd.area8sova.org/insanityworkoutonline19.asp cxrvl http://insanedvd.area8sova.org/insanityworkoutonline22.asp lcevk http://insanedvd.area8sova.org/insanityworkoutonline24.asp xmwba http://insanedvd.area8sova.org/insanityworkoutonline29.asp uxcsc http://insanedvd.area8sova.org/insanityworkoutonline30.asp duwbp http://insanedvd.area8sova.org/insanityworkoutonline33.asp zarur http://insanedvd.area8sova.org/insanity13dvd.asp zbjgk http://insanedvd.area8sova.org/insanity13dvdcheap..asp ftfmd http://insanedvd.area8sova.org/insanityworkoutdvdlist.asp ybevs http://insanedvd.area8sova.org/insanity13dvdsale.asp jlfmd http://insanedvd.area8sova.org/insanity13dvdtotalbodyworkoutsystem.asp kezfi http://insanedvd.area8sova.org/insanity13video.asp azhns http://insanedvd.area8sova.org/insanitycheaps.asp wzzke http://insanedvd.area8sova.org/insanitydeluxe13.asp dicsj http://insanedvd.area8sova.org/insanitydifference.asp baisc http://insanedvd.area8sova.org/insanitydvdaustralia.asp joenr http://insanedvd.area8sova.org/insanitydvds.asp nsmtc http://insanedvd.area8sova.org/insanityedition.asp aigay http://insanedvd.area8sova.org/insanitysale13dvd.asp qbcty http://insanedvd.area8sova.org/insanityshaunt.asp nugsh http://insanedvd.area8sova.org/insanityworkout13deluxe.asp ggywt http://insanedvd.area8sova.org/insanityworkoutasylumdeluxe.asp vfbud http://insanedvd.area8sova.org/insanityworkoutfitness.asp kfyye http://insanedvd.area8sova.org/insanityworkoutkit.asp cfyhx http://insanedvd.area8sova.org/insanityworkoutpackage.asp fxdop http://insanedvd.area8sova.org/shauntinsanity.asp tsbcc http://insanedvd.area8sova.org/upgradeinsanity.asp tguth http://insanedvd.area8sova.org/versioninsanity.asp

    Reply
  • 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 http://pandora.msvotes.org/ 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 http://fitnessdvd.area8sova.org/ lhpkn louis vuitton bag bagkz cheap louis vuitton bags vswst real louis vuitton bags jwcwk http://lv.smchillel.org/ kghyp karen millen dresses igadi karen millen dress dwjvt karenmillen xllmf dress karen millen woxbw http://karenmillendresses.10six.org/ wmzgy sac vanessa bruno vajyk cabas vanessa bruno qvroj vanessa bruno sac cuir esctz http://vanessabrunosac.msvotes.org/ cbhmf burberry outlet xeyqs burberry outlet online unzal cheap burberry bags ajnfu http://burberry.msvotes.org/

    Reply
  • hunters boots

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

    vzpiz http://insanedvd.area8sova.org/insanityworkoutonline2.asp fwgvt http://insanedvd.area8sova.org/insanityworkoutonline5.asp ryzoq http://insanedvd.area8sova.org/insanityworkoutonline8.asp ygnem http://insanedvd.area8sova.org/insanityworkoutonline11.asp hfqrp http://insanedvd.area8sova.org/insanityworkoutonline12.asp wqllx http://insanedvd.area8sova.org/insanityworkoutonline16.asp ijaju http://insanedvd.area8sova.org/insanityworkoutonline19.asp grcad http://insanedvd.area8sova.org/insanityworkoutonline23.asp mpnfg http://insanedvd.area8sova.org/insanityworkoutonline24.asp szgtg http://insanedvd.area8sova.org/insanityworkoutonline29.asp rauqz http://insanedvd.area8sova.org/insanityworkoutonline32.asp pwgap http://insanedvd.area8sova.org/insanity.asp qkzeb http://insanedvd.area8sova.org/insanity10dvdvs13dvd.asp jfixr http://insanedvd.area8sova.org/insanity13dvdcheap..asp qnqvu http://insanedvd.area8sova.org/insanityworkoutdvdlist.asp gymar http://insanedvd.area8sova.org/insanity13dvdsale.asp hzpzg http://insanedvd.area8sova.org/insanity13dvdtotalbodyworkoutsystem.asp ltmab http://insanedvd.area8sova.org/insanity13video.asp tsope http://insanedvd.area8sova.org/insanityday13.asp cttgw http://insanedvd.area8sova.org/insanitydeluxerutina.asp rpdzo http://insanedvd.area8sova.org/insanitydvd13.asp ibdcr http://insanedvd.area8sova.org/insanitydvddeluxeset.asp oruip http://insanedvd.area8sova.org/insanitydvds13.asp wdnks http://insanedvd.area8sova.org/insanityfreeshipping.asp cqekk http://insanedvd.area8sova.org/insanitysaledeluxe.asp rncms http://insanedvd.area8sova.org/insanityvsdeluxe.asp nqmvh http://insanedvd.area8sova.org/insanityvsdeluxevideos.asp mfcbk http://insanedvd.area8sova.org/insanityworkoutasylumdeluxe.asp rkdfv http://insanedvd.area8sova.org/insanityworkoutdvdlist.asp zqbxe http://insanedvd.area8sova.org/insanityworkoutkit.asp cfvet http://insanedvd.area8sova.org/insanityworkouts.asp ephwa http://insanedvd.area8sova.org/insanityworkoutsale.asp lqkgm http://insanedvd.area8sova.org/theinsanitysaledeluxe.asp pyvaa http://insanedvd.area8sova.org/usedinsanityworkoutdvdset.asp

    Reply
  • ghd australia

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

    ovacx burberry outlet fhnqh burberry bag kqdxo cheap burberry bags zzedq http://www.coutinhomusic.com/store/burberry.html xdqws insanity workout qmxwd insanity workout for sale xckuj insanity workouts zzuui http://beachbodysaleaustralia.info/ rmdah chaussures nike oqora nike chaussure cicce nike femme pas cher cades http://www.coutinhomusic.com/store/nike.html svhgd ghd australia bwfsh cheap ghd australia xdzwi ghd outlet lnwwh http://www.coutinhomusic.com/store/ghd.html jiurb karen millen dresses gcjot karen millen dress emgpg karen millen red dress ofkkg http://karenmillenoutlet-sale.info/

    Reply
  • 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 http://beachbodysaleaustralia.info/ brryx insanity workout phazr insanity workout australia zvjcs cheap insanity dvds izobi http://insanedvdprogram-au.info/ kvxig karen millen dresses ojwvd karen millen dress xooxv karen miller dresses qfmwb http://karenmillenoutlet-sale.info/ tmofy insanity workout ymwyc insanity workout sale naani insanity workout in stores omard http://beachbodydvdonsales.info/

    Reply
  • isabel marant

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

    tjtlx isabel marant sneakers qmott isabel marant sneakers sale goxwn http://isabellemaranthotsale.info/ okmhf insanity workout qyway insanity workout for sale knzdm insanity fitness xerxm http://fitnessbeachbody.info/ rdxdj insanity workout lbfom insanity workout australia vjcgt insanity 60 day workout cetag http://dvdsaleonline-au.info/ osxyv isabel marant rtrxe isabel marant chaussures ngnwz sneakers isabelle marant pdvpj http://iisabelmarantsoldes.info/ uyguh insanity workout bnlth insanity workout australia xfrvn insanity 60 day workout xadlv http://buyfitnessonline.info/ tvfzg ghd australia ouoaz cheap ghd straighteners ylnqk ghd factory outlet ktmrw http://hairaustraliatool.info/

    Reply
  • 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 http://fashionshoes-uk.info/ jfedc isabel marant hvpqd basket isabel marant cegqe chaussures isabel marant rnwzn http://pascherisabelmarantfr.info/ zhvbx isabel marant sneakers mwajr isabel marant wedge sneakers tbsoo http://sneakersisabelmarant-us.info/ urbrt isabel marant sneakers vzkhe isabel marant sneakers sale lzzwc http://isabellemaranthotsale.info/ piihk jimmy choo shoes uxlql jimmy choo sale hecbn jimmy choo shoes sale faexc http://jimmychoosaleus.info/ ytmqk insanity workout xqpnx insanity workout sale twnoj insanity dvd maxub http://dvdsaleonline-au.info/

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

RSS Feeds