| |
|
How to call COM components from .NET? What is interoperability?
COM components & .NET components have a different internal architecture. For both of them to communicate
with each other, inter-operation feature is required, this feature is called interoperability.
Enterprises that have written their business solutions using the old native COM technology
need a way for re-using these components in the new .NET environment.
.NET components communicate with COM using RCW (Runtime Callable Wrapper (RCW).
To use a COM component,
* Right click the Project & click on Add References.
* Select the COM tab
* Select the COM component
Another way of using a COM component is using the tblimp.exe tool (Type Library Import).
Using the COM component directly in the code may be achieved by using System.Runtime.InteropServices namespace.
This contains class TypeLib Converter which provides methods to convert COM classes and interface in to assembly
metadata. If the COM component does not have a Type Library, then custome wrappers need to be created.
Once the COM wrapper is created, it has to be registered in the registry.
How to call .NET component from COM?
In case a .NET component needs to be used in COM, we make use of COM Callable Wrapper (CCW).
Following are the different approaches to implement it:
* Explicitly declare interfaces.
* The second way to create CCW using InteropServices attributes.Here interfaces are created automatically.
Following are different type of class attributes :
None-- No class interface is generated for the class.This is default setting when you do not specify anything.
AutoDispatch-- Interface that supports IDispatch is created for the class. However, no type information is produced.
AutoDual-- A dual interface is created for the class. Typeinfo is produced and made available in the type library.
In below source code we have used the third attribute.
Imports System.Runtime.InteropServices
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class ClsCompliant
End Class
Other than class attributes defined up there are other attributes with which you can govern other part of
assembly.Example “GuidAttribute” allows you to specify the GUID,”ComVisibleAttribute “ can be used to hide .NET
types from COM etc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| |