COM Interop: Detail steps to create COM Interop Dll
Introduction
What is COM Interop?
COM Interop is a technology included in the .NET Framework Common Language Runtime (CLR) that enables Component Object Model (COM) objects to interact with .NET objects, and vice versa. COM Interop aims to provide access to the existing COM components without requiring that the original component be modified.
COM interop automatically provides the following services to simplify COM usage from .NET:
- Conversion between COM types and equivalent .NET types.
- Translation of retval arguments into return values.
- Translation of HRESULT return values into exceptions.
In addition, COM Interop allows COM developers to access managed objects as easily as they access other COM objects. Again, COM Interop provides a specialized utility (RegAsm.exe) that exports the managed types into a type library and registers the managed component as a traditional COM component.
In our Article, we will explain how to create COM interop using C#.net.
We will do simple example addition and subtraction.
COM Interop:
Client: Vb6
COM interop: C#.net
Launch Visual Studio
Create New Project
Select Visual C# - Class Library
Modify your AssemblyInfo.cs class as below
While modifying below lines, it will throw an error.
To solve this, add ‘using System.EnterpriseServices;’
[assembly: ComVisible(true)]
Create an interface class.
Example:
Interface.cs
How to create GUID?
Click on copy
Add below method in iInterface.cs (Addition and Subtraction)
Make sure our interface is public.
Copy paste below code
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace COMInterop
{
[Guid("7F10C7D5-6BC1-4ED1-BF50-6D37864404D6")]
public interface iInterface
{
int Addition(int a, int b);
int Subtraction(int a, int b);
}
}
Add new class name ‘ComInteropClass’.
Implement Interface iInterface.
Add below code in class ‘*ComInteropClass’
*Add reference *‘*using System.Runtime.InteropServices;’
Male sure class is public
Copy paste below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace COMInterop
{
[Guid("8D95AC43-E00F-4EFA-8F6E-7989071F96CB")]
public class ComInteropClass : iInterface
{
public int Addition(int a, int b)
{
try
{
return a + b;
}
catch
{
return 0;
}
}
public int Subtraction(int a, int b)
{
try
{
return a - b;
}
catch
{
return 0;
}
}
}
}
Go to Project properties – Build – Check the checkbox ‘Register for COM interop’
Build the application
Make sure there are no error and build successful
Next step to register the assembly
VS tool to register it
Locate to the release folderType:
attrib -r ComInterop.tlb
regasm.exe ComInterop.dll /codebase /tlb
*
*
Confirm that tlb file is created in release folder.
We have successfully created COM interop dll.
COM interop in registry:
Under ‘HKEY_LOCAL_MACHINE’
Now let’s create client in VB6.
Create new project
Add two text box, one button
Add reference of created assembly
Project -- References – Locate tlb file – Add
Copy paste below code
Option Explicit
Private Sub btnAddition_Click()
Dim comAssembly As iInterface
Dim a As Integer
Dim b As Integer
Dim c As Integer
Set comAssembly = New COMInterop.ComInteropClass
a = Val(txtFirstNumber.Text)
b = Val(txtSecondNumber.Text)
'Addition
c = comAssembly.Addition(a, b)
txtResult.Text = c
End Sub
Private Sub btnSubtraction_Click()
Dim comAssembly As iInterface
Set comAssembly = New COMInterop.ComInteropClass
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = Val(txtFirstNumber.Text)
b = Val(txtSecondNumber.Text)
'Subtraction
c = comAssembly.Subtraction(a, b)
txtResult.Text = c
End Sub
have successfully created client who are using COM+ server.
To unregister the COM+ application.
Type: regasm.exe /u ComInterop.dll /codebase /tlb we want to remove manual effort in Registering COM+, we can create batch file.
We hope this will help you to start with the COM Interop and get brief understanding regarding COM Interop.
Project is available at github.