COM in .NET

COM in .NET

  • .NET Interoperability
  • Abstract
  • Prerequisite
  • COM
  • .NET Interoperability
  • Runtime Callable Wrapper
  • COM Callable Wrapper
  • ActiveX
  • Conclusion
Abstract
 
This article begins by taking a brief backward glimpse at COM then compares it with the way that components interact in .NET and finally it takes a look at the tool provided by Microsoft to let these two technologies coordinate. Essentially, COM is the antecedent technology to .NET. COM defines a component prototypical where components can be written in dissimilar programming languages. In addition to that, they can be cast-off inside a process, across a process or across the network (DCOM). But the COM technology became more and more complicated in terms of implementation and turn out not to be extensible enough. However, the .NET justifies similar goals as COM had, but introduces new conceptions to make the job easier.
 
Prerequisites
 
In order to implement COM components interoperability with the .NET Framework, the following are required:
  • Visual Studio 6 with VC++ (optional)
  • Visual Studio 2010 with VC++ .Net features
COM
 
Before delving into .NET-COM interoperability, it is important to be aware of the legacy Component Object Model (COM) itself. COM was Microsoft's first attempt at creating a language-independent standard for programs. The idea was that interfaces between components would be defined or invoked according to a binary standard. This would have inferred that you could invoke a VC++ component from a VB application and vice versa. COM components implement one or more interfaces that define the various methods that an application may invoke during marshaling.
 
COM in Practice
 
To see how a .NET application can use a legacy COM component, you first need to create a COM component using VC++ because creating a COM component is not possible with C# or VB.Net.
  1. To create a COM component with ATL and C++, create a new ATL project. Here set the name to comServerTest and with the application setting, select Dynamic Linking Library and finally click Finish.
     
    COM component with ATL
     
  2. The first thing you need to define is a class, mathLib, then place the methods Addition() and Square() definition in the ImathLib interface. Once we have done with this, look at the comServerTest.idl file that shows all the method definitions as in the following:
    1. // comServerTest.idl : IDL source for comServerTest  
    2. //  
    3.   
    4. // This file will be processed by the MIDL tool to  
    5. // produce the type library (comServerTest.tlb) and marshalling code.  
    6.   
    7. import "oaidl.idl";  
    8. import "ocidl.idl";  
    9.   
    10. [  
    11.     object,  
    12.     uuid(DFC4EDDD-1115-4B36-90A3-15F547018FE8),  
    13.     dual,  
    14.     nonextensible,  
    15.     pointer_default(unique)  
    16. ]  
    17. interface ImathLib : IDispatch{  
    18.     [id(1)] HRESULT Addition([in] LONG val1, [in] LONG val2, [out,retval] LONG* val);  
    19.     [id(2)] HRESULT Square([in] LONG val1, [out,retval] LONG* val);  
    20. };  
    21. [  
    22.     uuid(F2DBF0C9-8136-4F38-BED2-333A365ED980),  
    23.     version(1.0),  
    24. ]  
    25. library comServerTestLib  
    26. {  
    27.     importlib("stdole2.tlb");  
    28.     [  
    29.         uuid(C670DD23-8B50-445D-9E53-17D6248EF1DA)        
    30.     ]  
    31.     coclass mathLib  
    32.     {  
    33.         [defaultinterface ImathLib;  
    34.     };  
    35. };  
  3. Now implement the business logic of those methods into the mathLib.cpp class as:
    1. STDMETHODIMP CmathLib::Addition(LONG val1, LONG val2, LONG* val)  
    2. {  
    3.     *val= val1+ val2;  
    4.     return S_OK;  
    5. }  
    6.   
    7. STDMETHODIMP CmathLib::Square(LONG val1, LONG* val)  
    8. {  
    9.     *val= val1 * val1;  
    10.     return S_OK;  
    11. }  
  4. Finally, build the component. The build process also configures the component in the registry.
     
    If you are developing your client .NET application on the same machine, you don't need to manually register it. However, if you are working entirely on a new machine, you need to register this component with the regsvr32.exe utility as in the following:
     
     build the component

.NET Interoperability

 
The mechanism where data is passed from COM components to .NET and vice-versa is called marshaling. The foremost concern during this process is the conversion of passed data types because both technologies have different data type definition schemes. But .NET and COM have a common representation for some data types such as int, long, short, and byte where no conversion is required during marshaling called Blittable data types.
 
In nonblittable data types, the conversion is required during the data pass because these data types have a higher overhead. The following table lists some nonblittable to related blittable types.
 
com type
 
You can use the previously created COM component comServerTest.dll from within the .NET application. We will implement the COM components methods within a Windows Form project. Here, first, we add the reference of the COM component then instantiate the MathLib class to invoke the methods.
  1. Create a C# Window Forms project named comServerTestClient.
     
  2. Design the form with the appropriate controls as in the following:
     
    com interoperability
     
  3. Add the COM component reference as in the following:
     
    add reference
     
  4. Place the following code for both button click events as in the following:
    1. using System;  
    2. using System.Windows.Forms;  
    3. using comServerTestLib;   
    4.   
    5. namespace comServerTestClient  
    6. {  
    7.     public partial class Form1 : Form  
    8.     {  
    9.         mathLib obj = new mathLib();  
    10.         public Form1()  
    11.         {  
    12.             InitializeComponent();  
    13.              
    14.         }  
    15.   
    16.         private void btnAdd_Click(object sender, EventArgs e)  
    17.         {  
    18.             int x = Int32.Parse(txtX.Text);  
    19.             int y = Int32.Parse(txtY.Text);  
    20.   
    21.              txtResult.Text = obj.Addition(x, y).ToString();    
    22.         }  
    23.   
    24.         private void btnSquare_Click(object sender, EventArgs e)  
    25.         {  
    26.             int x = Int32.Parse(txtX.Text);  
    27.              txtResult.Text = obj.Square(x).ToString();    
    28.         }  
    29.     }  
    30. }  
  5. Finally, compile and run the application.

Runtime Callable Wrapper

 
In the preceding example, we have done many activities. Every time you create a default interop assembly by importing a COM DLL into Visual Studio. So, it might be better to do a wrapping once and then let the application import the resulting .NET assembly instead of a COM component. The RCW would spare the hassle of repeatedly dealing with COM characteristics because it hides the IUnknown and IDispatch interfaces and deals with the reference count of the COM object.
 
RCW can be created by using the command line utility tlbimp.exe. Here, we are creating a new.dll from the COM component that contains a .NET assembly with the wrapper class as in the following:
 
Runtime Callable
 

COM Callable Wrapper

 
So far, we have seen how to access a COM component from a .NET application with RCW. Equally interesting is to find a solution for accessing a .NET component from the COM client using a COM Callable Wrapper (CCW).
 
In the following example, we create a simple C# class library project. Then add an interface to define methods that are invoked from a COM client.
  1. using System;  
  2. using System.Runtime.InteropServices;    
  3.   
  4. namespace NETcomponent  
  5. {  
  6.     [ComVisible(true)]  
  7.     public interface ITest  
  8.     {  
  9.         string Hello(string msg);  
  10.     }  
  11.     public class Test : ITest   
  12.     {  
  13.         public string Hello(string msg)  
  14.         {  
  15.             return "Hello " + msg;  
  16.         }  
  17.     }  
  18. }  
Now compile this class library project. Before the .NET components can be used as a COM object, it is necessary to configure it in the registry. You can use the regasm.exe utility to configure the components inside the registry as in the following:
 
COM Callable
 
Now open the Visual Studio command prompt to create a COM wrapper class using the tlbexp.exe utility as in the following:
 
COM Callable Wrapper
 
As you notice in the directory of the solution, the NewLib.tlb file is created after executing the tlbexp.exe command. You can view the type library with the OLE/COM object Viewer utility that resides in Visual Studio Tools as in the following:
 
cs code
 
In the generated code, you can see that the interface ITest is defined as a COM interface, and all the methods defined in the C# code are listed here in the type library.
 

Active-X

 
ActiveX controls are COM objects with a user interface. They are used by many containers, such as Word, Excel, IE, and Windows Forms. Similarly, to RCW, we can also create a wrapper for ActiveX controls using the aximp.exe command-line utility.
 
Active X
 

Conclusion

 
In this article, you have learned how the .NET application interoperates with COM and vice-versa using RCW and CCW. You also come across a couple of underlying utilities such as tlbimp to import a type library, tlbexp to export type library from .NET assemblies, and regasm and regsvr32 to register a .NET component in the registry. You also incorporate the ActiveX control into a .NET user interface.