.NET framework supplies developers with a tool to bridge their .NET applications with applications built with older technologies, such as COM.
What Will I Learn?
Building .NET Components for COM Applications
Requirements
- .NET framework
Difficulty
- Intermediate
Tutorial Contents
This tutorial will demonstrate how to build a simple Math class that implements InteropServices. The resulting C# dll will then be accessible to COM applications.
How to Build .NET Components for COM Applications
Developers can use the .NET library to build components with ease. However, older applications still use COM (Component Object Model Technology) for their underlying libraries. To enable .NET developers to interact with COM, Microsoft has bundled .NET with InteropServices, which acts as a sort of proxy wrapper that communicates between .NET and non .NET applications.
Using InteropServices
The first step is to create a new project and choose the New Class option, naming the class MyMathClass. At this stage C# will create a skeleton class to build upon. This class will eventually be fleshed out in detail to create the .NET component that the COM application will use.
Making Com VisibleHowever, before proceeding further, it’s important to note that projects intended for use with InteropServices need to be explicitly declared as such. This is done in several places in the project. The first place is in the AssemblyInfo.cs file, viewable in the solution explorer. There the following property appears:
[assembly: ComVisible(false)]
The false in this property needs to be changed to true. This tells .NET that the component project will be visible to the COM world, so that COM applications can use it. The next place to make a change is in the class definition itself. The following line should be added to the list of clauses at the top of the class definition: "using System.Runtime.InteropServices". This makes the InteropServices class available for use in the class.
Finally, a change needs to be made in the Project Properties window. Selecting Project, MyMathClass Properties will open this window. At the bottom of the Build tab window is a checkbox option "Register for COM Interop." This needs to be checked.
Presently the skeleton class looks like this.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MyMathClass
{
public class Math {}
}
C# Class
The class will contain one function, an Add() function to add two numbers. However, InteropServices lays down some rules which require additional work to be done than simply declaring the class member function as usual.
The first rule is that the class must implement its functions through an interface. Create a public interface above the class and name it MyMathInterface. Further, to interact with COM objects, the .NET class must reference COM’s specialized ComInterface as well. Without getting into the technical details, suffice it say that the class Interface needs to have the following line above it:
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
Then the class definition itself needs to have this line above it:
[ClassInterface(ClassInterfaceType.None)]
Finally, one last step for the class will make it completely ready for use. Both the interface and class definition needs to have their specialized GUID. You can find one online by going to Create Guid website.
The class should then look as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MyMathClass
{
[Guid("D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMathInterface
{[DispId(1)]
int Add(int firstNumber, int SecondNumber);
}
[Guid("E075A629-B6A5-47B9-B258-D5BB20BBA016")]
[ClassInterface(ClassInterfaceType.None)]
public class Math:IMathInterface
{
public int Add(int num1, int num2)
{
return num1 + num2;
}}}
The DispId(1) label before the function is needed to identify it as a public member to be used by the COM application. Any additional functions would need their own labels,—ie, DispID(2), DispId(3), etc. However, since only one function is defined here no additional labels are needed.
Before using the object, a strong key must be created. This is a cryptographic key required by .NET. Using the strong name key tool (sn.exe) supplied in the .NET SDK, create the key by typing the following at the command line:
sn –k MyMathKey.snk
Then in AssemblyInfo, reference the key file’s location like so:
[assembly: AssemblyKeyFile(@"C:\MyMathKey.snk")]
To test the new COM object, open up Microsoft Access, switch to Module view and then click Tools, References. From the list of references scroll down and then check MyMathClass.
In VBA code, type the following:
Sub Test()
Dim k As New MyMathClass.Math
Dim j As Integer
j = k.Add(3, 2)
MsgBox j
End Sub
The Message Box will display the results of the two numbers being added.
Developers who have invested significant time into COM software can continue to leverage that technology while transitioning into .NET. By using InteropServices, they can build .NET components to interact seamlessly with their existing COM applications.
Posted on Utopian.io - Rewarding Open Source Contributors