Containment Delegation  «Prev  Next»
Lesson 3 Integrating components: COM components
ObjectiveExplain how COM components integrate into an application.

How COM components integrate into an Application?

Integrating components COM Components

Although C++ classes are integrated at the source file, COM components are not. Let's examine how Elvis at ABC integrates with COM components provided by Component Builder, Inc. (CBI).
ABC has purchased a set of COM components from CBI. The COM components are provided as a set of header files that contain interface definitions, class IDs (CLSIDs), interface IDS (IIDs), and a DLL containing the components called CBIComp.dll, version v1.31.
Before using CBIComp.dll, Elvis runs regsvr32 to register all the COM objects contained in server CBIComp.dll. To use the object CLILookupObj, Elvis includes the appropriate header files to obtain interface definition ICLILookup and writes the following code:

void FindRec(...) {
  //Assume CoInitialize has been called
  CComPtr<ICLILookup> pICLILookup;

  //NOTE: If you are using VC++ 6.0, you can 
  //uncomment out this line and comment
  //out the "CoCreateInstance" line
  //hr = pICLILookup.CoCreateInstance((REFCLSID) 
  //             CLSID_CLILookupObj, NULL, 
  //             CLSCTX_INPROC_SERVER); 
  hr = CoCreateInstance(CLSID_CLILookupObj, 
                NULL, CLSCTX_INPROC_SERVER, 
                IID_ICLILookup, &pICLILookup);
   
  if (FAILED(hr)) ... ERROR processing and return

  hr = pICLILookup->Init(....);
  ...
}

After Elvis gets everything working, CLI ships a new version of its component library, v1.32. CBI has made only internal changes. For this scenario, all Elvis has to do is replace the old version of CBIComp.dll with the new one and reregister the server DLL. Elvis does not have to rebuild his code. Why? We will examine this in the next lesson.

Complex Software

COM is the smartest way of developing large and complex software. It is not related to a single programming environment. COM can be implemented in wide variety of programming environments like C, C++, Java, Visual-Basic and MFC. COM allows you to combine disparate software parts together at runtime. It makes software developers life easy as it mainly focuses on reusing existing tested components. It is heavily based on interfaces and programmer should implement the exact interface in order to connect to a particular component. The large amount of COM code can be found in DLL files. If you are using existing COM component then there is no big issue but if you are designing your own component then it must be registered in the windows registry. Component in COM means a language independent software module.

Aggregation

A part or all of the functionalities of a component can be made part of the collection of functionalities of another component through a mechanism called aggregation. This is a reuse scheme. A component that aggregates is called an outer component and the aggregated component is called an inner component. There are two kind of IUnknown interfaces for a component that can be aggregated (an inner component):
  1. delegating unknown and
  2. nondelegating unknown.
A delegating unknown is simple, it just returns the pointer to the outer component's IUnknown whenever a client queries it. A nondelegating unknown is just like a regular IUnknown, but only the outer component's IUnknown can access it.