COM Interoperability: Part I – Using COM in .NET


Part I – Using COM in .NET

COM is very deeply rooted in Microsoftproducts and it is not easy to neglect COM completely. There may besituations arise where you required to build COM in VC++ and you wantto use it in C# or even you want to use your old components. But don'tbe panic because as we all know Microsoft always support backwardcompatibility so same is here you can use your COM in .NET and evenyour .NET components in COM. I'll discuss here only using COM in .NET.

There are two ways to do it through Late Binding and Early Binding, VC++ developer must already know these terms.

Early Binding

Early Binding means compiler must have priorknowledge about COM, its functions and properties i.e. at compile time.So that Visual Studio can use Intellisense to remember you the methodsand properties supported by COM. What one has to do is simply addreference of COM through 'Add Reference' from 'Solution Explorer'.After adding reference, now you can use your COM just like .NETnamespace and by instancing your COM class as normal C# classinstantiation.

For example if you add a component named �DBAccess� by adding reference than you can include it in your project as

using DBAccess;

And you can create its class object like

DBAccess.DBFactory dbFactoryObject = new DBAcess.DBFactory();

Now you can use your component normally as

dbFactoryObject.getConnection();

Basically what Visual Studio .NET do is itcreates a Runtime Callable Wrapper (RCW) which is nothing but just asimple .NET proxy for COM which provides COM class functionality as.NET namespace by using COM type description. Visual Studio .NET isefficient enough that it automatically creates the proxy of otherdependent COM too. As in above example ADO 'dll' file must alsoincluded in the project which is done by VS .NET.

Late Binding

Late Binding means compiler doesn't have anyprior knowledge about COM's methods and properties and it is delayeduntil runtime. Infect program learns the addresses of methods andproperties at execution time i.e. when those methods and properties areinvoked. Late bound code typically refers client objects throughgeneric data types like 'object' and relies heavily on runtime todynamically locate method addresses.

We do late binding in C# through reflection.Reflection is a way to determine the type or information about theclasses or interfaces. We don't need to create RCW for the COMcomponent in late binding as we did in early binding. We use'GetTypeFromProgID' to get the type object for the COM object's typeinformation than we�ll use 'CreateInstance' to instantiate the COMobject of appropriate type.

Using System.Runtime.Interopservices;

Type objDBFactoryType;
objDBFactoryType = Type.GetTypeFromProgID(�DBAccess.DBFactory�);

object objDBFactory;
objDBFactory = Activator.CreateInstance(objDBFactoryType);

Now to invoke the COM method we need to callit through COM type object which we have created early. We have to passthe COM object's refernce. Method name and parameter list in form ofarray of objects.

objDBFactoryType.InvokeMember(�GetConnection�, BindingFlags.InvokeMethod,
null, objDBFactory, arInputArgs);

Although late binding seems to remove the complication of RCW but it requires more care and associated some overhead.

Related Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!