by Ravichandran J.V.
The issue in interoperability between two components of different platforms is not making the components interoperable but in succeeding in sending values from one environment to the other.
Visual Studio .Net is one of the most efficient tools to use when you wish to make COM component interoperable in .Net. It does the work in jiffy and the only knowledge that you need to have is how to add reference from your application to a DLL. The other tools to use are the Aximp.exe and the tlbimp.exe both being command line utilities.
The architecture of interoperability between .Net and COM components work on Wrapper classes. In either way interoperability, a wrapper class is created of the original DLL and the calling component is not aware that it is calling across boundaries.
There are two types of wrappers that come into play according to the type of interoperability desired; if interoperability is desired between a .Net component and a COM component, both in the same apartment models, then the Interop marshaller of the CLR is invoked else, if the Apartment model differs then the COM marshaler is invoked when CLR passes the call to the COM environment and COM invokes the COM marshaler to service the call. The default apartment state for .Net clients is MTA. But, as I said, the issue is not interoperability, the issue is passing data from one domain to the other.Since a component would be calling upon another component to obtain some service in the form of data, the issue, here, centers around making use of the data supplied by one component to the other. Passing data across boundaries is achieved through a technique called Marshalling.
Marshalling, in interoperability, chiefly depends upon the services of the two types of Marshallers - the Interop marshaller and the the COM Marshaller. Since the .Net types were built upon existing COM types, the CLR is well versed with the COM types known to it and hence, can manage the marshalling automatically. The keyword for automatic marshalling is known types, which are called blittable types, like the primitive data types. But there is an exception in the form of the String managed type. This type is a non-blittable type because string is represented in more ways than one, as LPSTR, BSTR, LPWSTR etc., in COM and hence, the runtime needs to know to which COM String type the data has to be marshalled to. To overcome this, .net interopservices provide for a MarshalAs attribute that marshals a managed type to a COM type.
Since .ocx format is not supported in .Net and none of the unmanaged types by the CLR, interoperability is basically an issue of providing metadata for the CLR through type generators like tlbimp.exe and aximp.exe, the latter being for ActiveX controls. But, although tools are available, one has to be careful on the two issues outlined above. One, the apartment model and the other, the data being passed.
If the COM type is built on STA and the .Net client references the COM type on a MTA model, an exception will be thrown that the apartmentstate is not compatible. The workaround for this is to modify the ApartmentState of the .Net client to STA.
To test the above, follow the steps below.
1. Open Visual Studio.Net and create a new Windows Application project in C# because in VC# the Main method is created with a Thread attribute. The language to be used as code behind language is not important so even if you do not know C# it does not matter.
2. Under Tools from the Main menu, Select Customize toolbox or Add/Remove Toolbox items as your version of VS.Net may be.
3. In the Dialog window that opens select the COM component tab if not selected and check any .ocx control from the list and click Ok.
4. The control is usually added to the General tab in the ToolBox.
5. Drag drop the control you selected onto your form.
6. Make some random call to some method of the imported class (If a Calendar control then a simple call to the _Value) or display the Microsoft RichTextbox's text property. Importing the icrosoft RichTextbox OCX control would make your test simpler.
7. Till now everything works fine.