By Faraz Ahmed Siddiqui
Part I - Using COM in .NET
COM is very deeply rooted in Microsoft products and it is not easy to neglect COM completely. There may be situations arise where you required to build COM in VC++ and you want to use it in C# or even you want to use your old components. But don't be panic because as we all know Microsoft always support backward compatibility so same is here you can use your COM in .NET and even your .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 prior knowledge about COM, its functions and properties i.e. at compile time. So that Visual Studio can use Intellisense to remember you the methods and properties supported by COM. What one has to do is simply add reference of COM through 'Add Reference' from 'Solution Explorer'. After adding reference, now you can use your COM just like .NET namespace and by instancing your COM class as normal C# class instantiation.
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 it creates a Runtime Callable Wrapper (RCW) which is nothing but just a simple .NET proxy for COM which provides COM class functionality as .NET namespace by using COM type description. Visual Studio .NET is efficient enough that it automatically creates the proxy of other dependent COM too. As in above example ADO 'dll' file must also included in the project which is done by VS .NET.
Late Binding
Late Binding means compiler doesn't have any prior knowledge about COM's methods and properties and it is delayed until runtime. Infect program learns the addresses of methods and properties at execution time i.e. when those methods and properties are invoked. Late bound code typically refers client objects through generic data types like 'object' and relies heavily on runtime to dynamically locate method addresses.
We do late binding in C# through reflection. Reflection is a way to determine the type or information about the classes or interfaces. We don't need to create RCW for the COM component in late binding as we did in early binding. We use 'GetTypeFromProgID' to get the type object for the COM object's type information than we’ll use 'CreateInstance' to instantiate the COM object 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 call it through COM type object which we have created early. We have to pass the COM object's refernce. Method name and parameter list in form of array 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.