Component Object Model (COM) is a method to facilitate communication between different applications and languages. COM is used by developers to create re-usable software components, link components together to build applications, and take advantage of Windows services. COM objects can be created with a variety of programming languages. Object-oriented languages, such as C++, provide programming mechanisms that simplify the implementation of COM objects. The family of COM technologies includes COM+, Distributed COM (DCOM) and ActiveXÂ® Controls.
COM (Component Object Model) was the first programming model that provided component based approach to software development. This component based approach of COM allowed us to develop small, logical reusable and standalone modules that integrates into a single application. But these components could not be displayed over a network. So these drawback produce another model that is DCOM (Distributed COM).
The DCOM programming model enables you to display COM components over a network and easily distribute applications across platforms. DCOM components also help in two-tier client/server applications. These models also have some drawbacks that help the development of the COM+ approach.
Creating COM components in .NET
The following steps explain the way to create the COM server in C#:
- Create a new Class Library project.
- Create a new interface, say IManagedInterface, and declare the methods required. Then provide the Guid (this is the IID) for the interface using the GuidAttribute defined in System.Runtime.InteropServices. The Guid can be created using the Guidgen.exe. [Guid("3B515E51-0860-48ae-B49C-05DF356CDF4B")]
- Define a class that implements this interface. Again provide the Guid (this is the CLSID) for this class also.
- Mark the assembly as ComVisible. For this go to AssemblyInfo.cs file and add the following statement [assembly: ComVisible (true)]. This gives the accessibility of all types within the assembly to COM.
- Build the project. This will generate an assembly in the output path. Now register the assembly using regasm.exe (a tool provided with the .NET Framework) - regasm \bin\debug\ComInDotNet.dll \tlb:ComInDotNet.tlb this will create a TLB file after registration.
- Alternatively this can be done in the Project properties --> Build --> check the Register for COM interop.
The COM server is ready. Now a client has to be created. It can be in any language. If the client is .NET, just add the above created COM assembly as a reference and use it.
How to call COM components from .NET?
COM components and .NET Components have a different internal architecture. For both of them to communicate with each other, the 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:- RCW Means Runtime Callable Wrappers, The Common Language Runtime (CLR) exposes COM objects through a proxy called the Runtime Callable Wrapper (RCW). Although the RCW appears to be an ordinary object to .NET clients, it's primary function is to marshal calls between a .NET client and a COM object.
To use a COM component,
Fig.1 Calling a COM component from .NET client
- Right click the Project and click on Add References.
- Select the COM tab
- And at last select COM component
Another way of using a COM component is using the tblimp.exe tool (Type Library Import). How to call .NET component from COM?
In case a .NET component needs to be used in COM, we make use of CCW (COM Callable Wrapper).
CCW:- .NET components are accessed from COM via a COM Callable Wrapper (CCW). This is similar to a RCW, but works in the opposite direction. Again, if the wrapper cannot be automatically generated by the .NET development tools, or if the automatic behavior is not desirable, a custom CCW can be developed. Also, for COM to 'see' the .NET component, the .NET component must be registered in the registry. CCWs also manage the object identity and object lifetime of the managed objects they wrap.
Fig.2 Calling a .NET component from COM client
Following are the different approaches to implement it:
- Explicitly declare interfaces.
- The second way to create CCW is using InteropServices attributes. Here interfaces are created automatically.