Exposing COM Interfaces of a .NET Class Library for Late Binding

Introduction

For my last project, I developed a nice library in VB.NET for credit card processing. It used sockets and the message format between our servers and those of Citibank was based on a variant of ISO 8583. Calling this library from ASP.NET pages was working fine. But, my Project Manager wanted it to be called from some old VB 6 and classical ASP applications, too. So, this scenario demanded that there should be some sort of COM interface to access that component. Also, our Tech. Lead wanted to use Late Binding (i.e. "CREATEOBJECT (PROG ID)") to instantiate this component/class library.

So, I was asked to build a COM interface for this class library. I did some research and found some very useful articles that dealt with the same problem. However, I found them somewhat difficult to follow. I managed to complete my development using those articles. Now, I want to to share my own experience in a somewhat easier manner (at least I believe so) to make life easier for other developers. The only prerequisite for this article is that you should have Microsoft Visual Studio .NET and should have some basic knowledge of COM.

The Problem

Obviously, I cannot present the actual class here. Instead, I will use a very simple class for demo purposes. This class is written in VB.NET as a Class Library. Now, the requirement is that it is to be accessed from clients (VB 6, classic ASP or even ASP.NET application using Late Binding) with

CreateObject (PROG ID of the component).

If you follow this example, you can apply the same procedure for very complex classes too.

Public class demo
   Private csError As String    ' stores the error message
   Public ReadOnly Property ErrorMsg() As String
      Get
         Return csError
      End Get
   End Property
   Public Function Concat(ByVal str1 As String, ByVal str2 _
                          As String) As STRING
      return Concat = str1 + " " + str2
   End function

End class

This class demo which has a property ErrorMsg and one function Concat, built as a class library project using Visual Studio.NET, has been given a Strong Name using the strong name tool sn.exe. It is important to note that you can avoid strong naming your assembly. However, I would advise you against it. If you do not want to use the strong name, then keep your component and the calling client in the same directory.

The Solution

As I found out, there are two ways to solve this problem. One is very easy while the other one is a bit hard. Unfortunately, I came to know the hard one first and discovered the second one after I had implemented my component using the hard method.

Solution 1 : To create a COM object using the COM class template of Visual Studio.NET

The easiest way to create COM objects is by using the COM class template of Visual Studio.NET. The COM class template creates a new class, then configures your project to generate the class as a COM object and registers it with the operating system automatically. All the background work will be done by Visual Studio (how nice !!). Here are the steps involved using Visual Studio.NET:

  1. Open a New Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box will appear.
  2. With Visual Basic Projects highlighted in the Project Types list, select Class Library from the Templates list, and then click OK. The new project will be displayed.
  3. Select Add New Item from the Project menu. The Add New Item dialog box will be displayed.
  4. Select COM Class from the Templates list, and then click Open. Visual Basic.NET will add a new class and configure the new project for COM interop.
  5. Add code, such as properties, methods, and events to the COM class. In our case we will just copy the code for property ErrorMsg and method Concat into the class and rename the class to class demo.
  6. Select Build Solution from the Build menu. Visual Basic.NET will build the assembly and register the new COM object with the operating system automatically.

What's next? Nothing, folks. Your COM component is ready to be accessed from any VB or classic ASP page etc. using Createobject(...).

Solution 2: Do-it-yourself approach

This is a little bit difficult as compared to method number 1. But, if you are a programmer who likes to go little bit deeper into details, you will be inclined to go this way. Some basic knowledge about COM is required. Once you complete all the steps for this method, your class will look quite similar to this one.

Imports System.Runtime.InteropServices

<Guid("1F249C84-A090-4a5b-B592-FD64C07DAB75"), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface _demo
   <DispId(1)> Function Concat(Byval s1 as string, _
                               ByVal S2 As String) as string
   <DispId(2)> readonly Property ErrorMsg() as string 

End Interface

<Guid("E42FBD03-96DF-43a7-A491-23E735B32C5C"), _
ClassInterface(ClassInterfaceType.None), _
ProgId("comDemo.demo")> Public Class demo
   Implements _demo
Private csError As String ' stores the error message
   Public ReadOnly Property ErrorMsg() As String
implements _demo.errormsg
      Get
         Return csError
      End Get
   End Property
Public Function Concat(ByVal str1 As String, _
                       ByVal str2 _As String) As STRING
implements _demo.Concat
return Concat = str1 + " " + str2
end function
end class

STEPS INVOLVED:

  1. Add the interop namespace to your class
    Imports System.Runtime.InteropServices
  2. Then develop an interface with the same name as of your original class, just add _ before it to make it different. For our example, it will be like this:
    Public Interface _demo
        
    
    End Interface
    
  3. Then, create a GUID for this interface. To create GUID for your COM object, click Create GUID on the Tools menu, or launch guidgen.exe to start the guidgen utility. Search for the file guidgen.exe on your machine. It is located under c:\program files\ Microsoft visual studio .net\common7\ tools directory on my computer. Run it. Then, select Registry Format from the list of formats provided by the guidgen application. Click the New GUID button to generate the GUID and click the Copy button to copy the GUID to the clipboard. Paste this GUID into the Visual Studio.NET code editor. Remove the leading and trailing braces from the GUID provided. For example, if the GUID provided by guidgen is {2C8B0AEE-02C9-486e-B809-C780A11530FE} then the GUID should appear as: 2C8B0AEE-02C9-486e-B809-C780A11530FE. Once a guid is created, paste it into the class as:
    Guid("1F249C84-A090-4a5b-B592-FD64C07DAB75")
    Then use the interfacetype attribute to make it a Dispatch interface required by automation as:
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)
    Unless you specify otherwise, the export process converts all managed interfaces to Dual interfaces in a type library. Dual interfaces enable COM clients to choose between Early and Late Binding.You can apply the InterfaceTypeAttribute attribute to an interface to selectively indicate that the interface should be exported as a Dual interface, an IUnknown-derived interface, or a dispatch-only interface (dispinterface). All exported interfaces extend directly from either IUnknown or IDispatch, regardless of their inheritance hierarchy in managed code.
  4. In the interface, expose the methods, properties etc that you want the clients to see and access. In our case, we will expose both our property and our method and will assign arbitrarily values of 1 and 2 as dispids. Our interface will look like this at this point of time
    <Guid("1F249C84-A090-4a5b-B592-FD64C07DAB75"), _
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
    Public Interface _demo
       <DispId(1)> Function Concat(Byval s1 as string, _
                                   ByVal S2 As String) as string
       <DispId(2)> readonly Property ErrorMsg() as string
    
    End Interface
  5. In a similar fashion, you need to create a GUID for the actual class too.
    Guid("E42FBD03-96DF-43a7-A491-23E735B32C5C")
    Next, use the interop attributes as follows:
    ClassInterface(ClassInterfaceType.None), _
    ProgId("comDemo.demo")> Public Class demo
       Implements _demo
    
    You must have noticed that you have to specify the ProgID for the class here. This is the same ProgID that we will be using to call our component as follows;
    set myobject = createobject ("comDemo.demo")
       ' vb6 or vbscript
    

    implements _demo suggests that the class is implementing the interface. Note that we are using classinterfacetype.none for the classinterface attribute. Microsoft states that

    "To reduce the risk of breaking COM clients by inadvertently reordering the interface layout, isolate all changes to the class from the interface layout by explicitly defining interfaces."
    Use the ClassInterfaceAttribute to disengage the automatic generation of the class interface and implement an explicit interface for the class, as the following code fragment shows:

    <ClassInterface(ClassInterfaceType.None)>Public Class LoanApp
       Implements IExplicit
       Sub M() Implements IExplicit.M
       ...
    End Class
    
    The ClassInterfaceType.None value prevents the class interface from being generated when the class metadata is exported to a type library. In the preceding example, COM clients can access the LoanApp class only through the IExplicit interface."
  6. The next step is to modify the properties and methods signatures which we want to expose. So,
    Public ReadOnly Property ErrorMsg() As String
    becomes
    Public ReadOnly Property ErrorMsg() _
           As String implements _demo.errormsg
    
    and
    Public Function Concat(ByVal str1 As String, _
                           ByVal str2 As String) As String
    
    becomes
    Public Function Concat (ByVal str1 As String, _
                            ByVal str2 As String) _
                           As String implements _demo.Concat
    
  7. Once you make these changes to your class, you are all set. Just one more change is required to make it complete. Right-click the project to bring the project properties, they're under the Build properties check the interop check box. When building the project, this will ensure that a type library is created for the COM Interface and is registered too. If you do not want to use this option, you can use the Regasm utility to do the same. For Regasm, the syntax would be
    regasm demo.dll /tlb: demo.tlb
    It will create the type library and will register it to for you.

That Was It

You can build the project and can access it:

  1. Through Late binding: using createobject("comDemo.demo") or
  2. Through Early binding: by setting reference to this component in your project

Strong Name Requirement

It is recommended that your assembly should have a strong name. For that, you can use the strong name tool sn.exe. If you do not create the strong name, you will have to keep the component and the calling program in the same directory.



About the Author

Muhammad Musa Ali

MCAD, MCSD - Early Achiever in .Net, has more than seven years of experience in software development and system analysis, currently working as a consultant for a fortune 500 company in Chicago.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds