Classic COM to .NET Interoperability
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft .NET SDK
.........................................Microsoft Visual Studio.NET Beta 2 or higher

The Problem

To make a COM Client use a .NET type component Server as though it were accessing another regular COM Server, we have to expose a COM coclass and make the client believe that the managed .NET type component is somehow unmanaged!!! This means, there has to be a mechanism to interact with the .NET type component using the functionality provided by COM. This also means, the COM Client should be able to obtain new interfaces through QueryInterface() calls, mimic unmanaged memory management using AddRef() and Release(), make use of the COM Connection Point protocol, etc.

In addition to fooling the COM Client, COM to .NET interoperability also has to simulate COM functionality to the COM Runtime. For instance, a Classic COM Server is activated using the Service Control Manager (SCM). This means, when the SCM looks-up for numerous bits of information in the system registry (like ProgID, CLSID, IID, etc.), the information is available for it to use. The main problem is that .NET assemblies are not registered at all.

The Rules

To make .NET assemblies available to classic COM Clients, the following has to be done:

  1. .NET Assemblies have to be registered into the System Registry to allow the COM SCM to locate them.

  2. A COM Type Library (.tlb) file has to be generated using the .NET metadata to allow COM clients to interact with exposed COM type components.

  3. The Assembly has to be deployed in the same directory as the COM Client, or has to be installed into the Global Assembly Cache (GAC).

The Solution

When COM Clients access a .NET Type Component that's masquerading as a COM type, the .NET Common Language Runtime (CLR) uses a proxy called the COM Callable Wrapper (CCW) to negotiate "COM to .NET communication" as shown in the figure below.

The Classic COM Client assumes that the CCW Proxy object is a real COM object and assumes that it abides by the rules of AddRef() and Release(). The CCW in reality, is a reference counted entity. When the COM Client has issued the final release, the CCW Proxy releases its reference to the real .NET Component, at which point the .NET Component is ready to be garbage collected, just as a Classic COM Server object would.

To further the illusion that it represents an actual COM coclass, the CCW implements a number of COM interfaces automatically, in addition to the set of custom interfaces defined by the .NET Component.

To illustrate Classic COM to .NET Interoperability, we develop a Classic COM Server using C# and .NET. We then develop Classic COM client applications using Visual Basic ver 6.0 which accesses our server. In addition we also demonstrate how to access our server with Managed C++ Clients using both early, and late binding.

Classic COM to .NET Interoperability
Developing a Classic COM Server Component using C# and .NET - The QuoteServer Example
Developing a Classic COM Client Application using Visual Basic ver 6.0 to access the QuoteServer - The VB6Client Example
Developing a Managed C++ Client Application which binds to the Server using Early Binding - The QuoteClient Example
Developing a Managed C++ Client Application which binds to the Server using Late Binding - The cppLateBinding Example

 

 

Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since December 27, 2001.

Last Updated : Dec 27, '01

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2001, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.