Click here to Skip to main content
13,288,306 members (51,155 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 1 Feb 2007

Aggregation explained

, 1 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Article describing the steps involved to attain aggregation relation between COM components

Sample image


Let's have a look at the reuse technique "Aggregation" and how we can implement this in COM.


In this reuse mechanism, The Outer COM directly exposes the Inner COM's interfaces to the client. I will try to explain the steps involved to attain Aggregation relation between the InnerCom and OuterCom in the simplest way possible, so that beginners in COM can understand and workout.

Let's work on an example to establish aggregation relation between the Calculator Component (InnerCom), which already is available with us and ComplexCalculator, which we are going to build now.

Download the Calculator.dll from the source and register it.


  • Create a new Project of type Atl COM, name it ComplexCalculator and select the type as "dll".
  • Insert a new Atl Object, select the Simple Object option and Name it ComplexMath.
  • Add a new method to the IComplexMath interface.

Method Name: Divide

Parameters: [in] int x, [in] int y, [out, retval] int* z

Add the following code:

STDMETHODIMP CComplexMath :: Divide (int x, int y, int *z)
    *z= x/y;

    return S_OK;

Do the following changes to CalculatoeEx.idl:

Copy the object section and the interface section of the Calculator.idl to ComplexCalculator.idl

    helpstring("ISimpleMath Interface"),


    interface ISimpleMath : IDispatch
        [id(1), helpstring("method Add")] 
            HRESULT Add([in] int x, [in] int y,    
            [out, retval] int* z );

In the coclass part of ComplexCalculator.idl, add the interface name that we want to aggregate.

Note: Changes that need to be done are indicated in bolds.

coclass ComplexMath
    [default] interface IComplexMath;
    interface ISimpleMath;

Do the following changes to the ComplexCalculator.h:

Copy the CLSID of the SimpleMath from Calculator_i.c to ComplexCalculator.h

const CLSID CLSID_SimpleMath = {0xF58218E0,0x645A,0x473D,

Do the following change in the COM MAP.

Note: Changes that need to be done are indicated in bolds.


Add the following macro, finalconstruct and release methods

IUnknown* ptrUnk;

HRESULT FinalConstruct()
    return CoCreateInstance(CLSID_SimpleMath,
            IID_IUnknown, (void**)&ptrUnk);

void FinalRelease()

Search for "Aggregation" in the source code to find code that is inserted to build the relationship between the "Calculator" and "ComplexCalculator".

Note: Register the Calculator.dll and ComplexCalculator.dll before using the demo project.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Questionrofl who is the sob unit goat humper who posted this trash Pin
Member 125092437-May-16 15:08
memberMember 125092437-May-16 15:08 
GeneralThanks Pin
Kamal Ramya Santharam19-Sep-08 2:15
memberKamal Ramya Santharam19-Sep-08 2:15 
GeneralInvoking from VBScripts Pin
clementemersona5-Sep-08 23:12
memberclementemersona5-Sep-08 23:12 
GeneralThanks Pin
kev~15-May-07 2:44
memberkev~15-May-07 2:44 
Very helpful, exactly what I needed to know. Dunno why this information is so hard to find elsewhere on the web.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 1 Feb 2007
Article Copyright 2007 by Ramchandra.kk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid