C#.NET and COM Interop

This article discusses COM and C# Interoperability. It is divided into two parts: Part I Introduction About COM, Part II How to use COM components in .NET Framework

SUMMARY : This article explains about COM and C# Interoperability with COM.This article is divided into two parts: Part I Introduction About COM, Part II How to use COM components in .Net Framework

PartI:

Introduction To Component Object Model(COM).

COMPONENT:

Component is nothing but the reusable piece of software in binary form that can be plugged into other components from other vendors. Reusable software programs that share a common interface allowing easy integration into applications that is it is a Binary standard that allows any two components to communicate that component may be written in any language.

NEED FOR COMPONENT:

Today’s applications are time consuming to develop, difficult and costly to maintain and we cannot remove a feature, upgraded or replaced with alternatives. The solution for this problem is reusable software components.

SOLUTION:

The solution to this problem is to isolate the functionality that is required by many applications and creates a new application from it and makes into a common functionality. Each time the client requires the functionality then it should be called and uses its features as though they were its own. In our project we create the Components from the five applications so that the users can easily use it in many languages and upgrade it easily.

COM FRAME WORK

To create application that can interact with any other application requires that there be a standard manner in which the two applications will interact. So, for that we need a framework that defines the rules that will enable to interact with each other irrespective of who has created the application.

COM AS BLUE PRINT:

If all the application follows the same rules for interaction then application created by different people can interact with each other and we can exploit any application to obtain the feature we need. Microsoft has defined a model that sets standard that will enable application to interact with each other. This model is referred to as the COM. It defines the rules which when implemented will enable applications to interact with other application in a uniform manner. Both calling application and the called application need to follow the rules specified by the COM. Thus COM acts as blue print.So Com just defines the theory but not how they are to be implemented.

MICROSOFT’S COMPONENT OBJECT MODEL (COM)

When we register a COM or DCOM object within the window registry, we must reference that object’s Globally Unique Identifier (GUID), which is a 128-bit (16-byte) number that uniquely identifies the object. Whenever a client program accesses a COM or DCOM object, the client program will use the GUID to reference the object within the windows Registry. Another powerful COM feature is language independence. COM clients and COM objects have the same layout and behavior at run time, regardless of which language we use to produce the component. COM’s independence from any specific language lets build systems with many different components that other programmers create in different language. The language we use to create the component simply does not matter in COM. First, it helps in split up large systems into manageable subsystems early in the design phase. Second, it assists to implement each subsystem with a component that we create with any Com-capable tool. Third, it lets individual teams working on each subsystem has complete. The two most notable of these technologies are OLE (Object Linking and Embedding) and ActiveX. These both use COM to facilitate all the interaction between objects. OLE uses COM to communicate between applications, allowing users to link or embed parts of one application's data and display into another application.

INTERFACES OF A COMPONENT:

MICROSOFT’S INTERFACE DEFINITION LANGUAGE (IDL)

The fundamental concept behind both COM and DCOM is the interface. An interface is an agreement between a client and an object about how they will communicate with each other. When we define interfaces in languages other than the visual basic, we have to use Microsoft’s Interface Definition Language (IDL), which we must compile within the Microsoft Interface Definition Language Compiler (MIDL).

GLOBALLY UNIQUE IDENTIFIER (GUID)

Each interface that we define in a COM object will include a Universally Unique Identifier (UUID), which the OS constructs exactly as it does a Globally Unique Identifier. When client programs access an interface the COM object exposes, the programs will actually reference the interface’s UUID. An interface is an agreement between a client and an object about how they communicate. The interface becomes a communication channel between the client and the object. Within our program code, an interface is a collection of related procedures and functions.

FUNCTIONS OF INTERFACES

When we create objects that we build around COM and clients to communicate with the COM objects, both the clients and the objects must communicate exclusively through interfaces. An interface between two objects is actual program code. A logical interface is the model for the program code itself. In addition, an interface is a communications protocol that defines a set of methods complete with names, arguments and return type.A COM object must implement at least one interface, although the object is free to implement as many interfaces as it requires. The object creates an array of function pointers, called a V table (the “V” stands for Virtual), and passes a V Table pointer to the client. The client sees the V Table as the interface and uses the V Table pointer it received from the client to locate a particular function pointer.when the client finds the function pointer, the client invokes the method directly. In other words, when programs access the COM object, they will always access the V Table and receive a vptr from the V Table’s. When we compile the COM-based project, the compiler feeds the Interface Definition Language file to the Microsoft Interface Definition Language (MIDL) compiler, produces a binary description file called a type library.

Part II:

Accessing COM Components from C# Managed Code

If we want to access the existing COM+ application with any .net supported language, we don't need to modify the single line of exisiting COM+ application, despite of the fact their execution model is completely different. We can access it in two ways, Early Binding and Late binding (remembered the old VB concepts). We will first look at Early binding example.

Early Binding:

Tools Required:
TlbImp.exe: (Type Library Importer) Imports (converts, generates wrapper DLL) the type defination found in COM component into equivalent .net definations (or metadata), understandable by Common Language Runtime (CLR). The metadata generated, by TlbImp can be viewed by Ildasm.exe. If you are using the Visual Studio development environment, you only need to add a reference to the COM type library and the conversion is done automatically.

Let's Start:
To use exisiting COM+ application we need create Runtime Callable Wrapper (RCW) using
TlbImp.Exe, with VS.Net it is created automatically, when we reference the exisiting COM Component. The difference is that, VS.Net create the RCW with the same name as the original DLL, which may be confusing, and with TlbImp.Exe, we can specify the different name using /out: parameter.

Let us assume that we have a COM Component with a method Add, which takes two parameter A and B and returns the Sum.

Note: We have to register COM DLL first before using it...

C:\ravi\COM-DCOM-ATL\CallDLL_From_Console_src\dll4C\ReleaseUMinDependency>TlbImp

dll4c.dll /out:COMRCW

Microsoft (R) .NET Framework Type Library to Assembly Converter 1.1.4322.573

Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Type library imported to C:\ravi\COM-DCOM-ATL\CallDLL_From_Console_src\dll4C\ReleaseUMinDependency\COMRCW.dll

Late Bindings:

For Late binding we have to use System.Reflection namespace, which allows us to programmaticaly access the types contained in any assembly. We will see how we can do late bindings in four easy steps..

  • We have Get IDispatch Interface using Type.GetTypeFromProgID("Project1.Class1")
  • We have to create instance using the type ID Activator.CreateInstance(objAddType)
  • We have to make array of arguments (if required)
  • Invoke the Method using objAddType.InvokeMember function.

By Ravi Sankar   Popularity  (12882 Views)