Click here to Skip to main content
12,953,007 members (70,781 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 12 Nov 2003

COM Concept : Containment

, 12 Nov 2003
Rate this:
Please Sign up or sign in to vote.
COM Concept : Containment


Reusability is the mantra for the success of any technology in the field of programming. C++ supports the reusability by providing the concept of inheritance. Inheritance is the way by which the subclass inherits the functionality from its base class (parent class). The subclass has the option to modify the functionality, which has been provided by the base class (overriding), or to continue with the same functionality as that of the base class. Inheritance has made the life of the programmer’s easier, but this has a problem associated with it. Implementation inheritance, which is supported by C++, creates a strong bonding or the contract between the base class and a subclass. Any changes in the base class can cause a drastic affect on the child class and may cause the clients of the child class to break. Implementation Inheritance is not an appropriate technology to create a reusable components, which could be used anywhere, at anytime, by anybody without worrying about the internal implementation of the component.

COM doesn’t supports the Implementation inheritance as it violates the motto of the COM technology i.e. to create the reusable components. COM does supports the Interface inheritance, in which the subclass inherits the interface of the base class and not the implementation. Interface inheritance protects the clients of a component from change. Implementation inheritance can be simulated in COM by using the concept of component containment. In COM, the reusability is achieved by using containment and aggregation. This article will be covering the Containment technique, in which the outer component uses the inner component, in detail. The Aggregation will be covered in a separate article


Everything in COM is related with the interfaces. Containment is also implemented at the interface level. The COM containment is same as the C++ containment, in which the outer component is a client of an inner component. The outer component has pointers to interfaces on the inner component. The inner component is not exposed directly to the client and hence only the IUnknown of the outer component will be exposed to the client. In Containment, the outer component forwards (delegates) the calls to the inner component.

There could be two scenarios in which the containment can be implemented. The first case is the outer component implements its own interfaces and uses the interfaces of the inner component. The second case could be that the outer component reimplements an interface supported by the inner component and forward the call to the interface of the inner component.

In Containment, the outer component is acting as a client and using the interface of the inner component. In implementing the containment, the inner component and client are unaware of the fact that they are being the part of the containment implementation. The outer component has to be modified to support the containment.

Sample code - explanation

This article will explore the first scenario to explain the containment technique. In this sample code, the outer component utilizes the functionality, which is being provided by an inner component. The outer component needs some modification to accommodate the inner component as a contained object. The client and an inner component won’t be affected and will be unaware of the fact that they are taking part in the containment implementation. This sample code will demonstrate that the client is unfamiliar with the fact that the outer component is using the services of an inner component.

The outer component i.e. CMath will have a new member variable m_pISqaure, which is a pointer to ISquare interface on the inner component.

// Code Snippet for CMath class.

  class CMath : public IMath {
  // Implementing IUnknown Interface.

    virtual HRESULT __stdcall QueryInterface(const IID& iid,void **ppv);
    virtual ULONG __stdcall AddRef();
    virtual ULONG __stdcall Release();

  //Implementing IMath Interface.
    virtual void _STDCALL SumSquare(int Val1,int Val2,int* pResult);

  // Constructor

  // Destructor

  // Pointer to ISquare interface on the inner component.

    ISquare* m_pISquare;
    long m_cRef; 

As this COM Server (ContainmentSample.dll) supports two COM Components i.e. CMath and CSquare, therefore DllGetClassObject should have a validation for these two ClassIDs.

 // Code Snippet for DllGetClassObject.

 // COM SCM creates a class object only when the request has come for
 // CLSID_CMath and CLSID_CSquare. After creating the
// class object, the IClassFactory interface pointer on the
// class object is returned back to the client.

 STDAPI DllGetClassObject(const CLSID & clsid,const IID& iid,void **ppv)
    // This server supports two COM Components and hence
    // validation is performed.

    if((clsid == CLSID_CMath) || (clsid == CLSID_CSquare)) {
        cout<<"The requested component is supported by "
           "COM server (ContainmentSample.dll)" << endl;
    CFactory *pFactory = new CFactory();
    if (pFactory == NULL) {
        return E_OUTOFMEMORY;
    HRESULT hResult = pFactory->QueryInterface(iid,ppv);
    static_cast<< IUnknown* >>(pFactory)->Release();
    return hResult;

The CreateInstance for the outer component has to be modified to accommodate the creation of an inner component and storing the ISquare interface on the inner component in its member variable i.e. m_pISqaure. The outer component calls the CoCreateInstance with the CLSID parameter as CLSID_CSquare and queries for ISquare interface on the inner component (CSquare), and if the call is succeeds it stores the interface pointer in m_pISqaure.

//Code Snippet for CreateInstance of Class Object.

//This snippet shows the part of the code which is
//executed during the creation of an outer

//This is executed when the client calls the CreateInstance,
//after getting the IClassFactory
//interface pointer on CMath's instance.
//The client gets the IClassFactory interface pointer by
//calling CoGetClassObject.

if ((iid == IID_IMath) || (iid == IID_IUnknown)) {
    CMath* pMath = new CMath();
    if(pMath == NULL) {
        return E_OUTOFMEMORY;
    // Here, the Outer Component initializes the inner component.
    // The CoCreateInstance is called by
    // the outer component during its creation and
    // it queries for the ISquare interface on the inner
    // component, and if the calls succeeds it stores the pointer
    // in its variable m_pISqaure.

  cout<<"Call to Create the Inner Component" << endl;

    hResult = CoCreateInstance(CLSID_CSquare,NULL,CLSCTX_INPROC_SERVER,

  cout<<"CoCreateInstance for CSquare has been called"<< endl;

  hResult = pMath->QueryInterface(iid,ppv);

  if(SUCCEEDED(hResult)) {

Before executing the client application, the COM Server (ContainmentSample.dll) needs to be registered by regsvr32 utility. The next article will cover the aggregation technique, which is a specialized case of the containment.


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

Dinesh Ahuja
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralActiveX Question Pin
charfeddine_ahmed10-Jul-06 21:40
membercharfeddine_ahmed10-Jul-06 21:40 
GeneralSecond Case Pin
Henrik L19-Dec-03 3:54
memberHenrik L19-Dec-03 3:54 
GeneralThis is not a COM concept Pin
Jörgen Sigvardsson14-Nov-03 8:05
memberJörgen Sigvardsson14-Nov-03 8:05 
GeneralRe: This is not a COM concept Pin
Normski14-Nov-03 22:18
memberNormski14-Nov-03 22:18 
GeneralRe: This is not a COM concept Pin
Anonymous15-Nov-03 0:08
sussAnonymous15-Nov-03 0:08 
GeneralRe: This is not a COM concept Pin
Jörgen Sigvardsson15-Nov-03 0:35
memberJörgen Sigvardsson15-Nov-03 0:35 
GeneralRe: This is not a COM concept Pin
vertigo00oo19-Nov-03 5:12
membervertigo00oo19-Nov-03 5:12 
GeneralRe: This is not a COM concept Pin
ledz19-Nov-03 6:53
memberledz19-Nov-03 6:53 
GeneralRe: This is not a COM concept Pin
vertigo00oo19-Nov-03 7:01
membervertigo00oo19-Nov-03 7:01 
GeneralRe: This is not a COM concept Pin
Dinesh Ahuja19-Nov-03 17:10
memberDinesh Ahuja19-Nov-03 17:10 
GeneralRe: This is not a COM concept Pin
BlueLi3-Mar-04 16:40
memberBlueLi3-Mar-04 16:40 

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
Web01 | 2.8.170525.1 | Last Updated 13 Nov 2003
Article Copyright 2003 by Dinesh Ahuja
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid