OOPortal OOPortal

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'll examine this in the next lesson.