COM Interoperability – Part II

COM Interoperability

Communication between Visual Basic and C# with events

In this tutorial, we’ll delve into the fascinating world of COM Interoperability, demonstrating seamless communication between Visual Basic 6 (VB6) and C# applications. Throughout the tutorial, you’ll learn the crucial steps to establish this communication channel, unlocking the potential for VB6 and C# applications to work in harmony. You’ll witness the power of COM Interoperability as the C# library displays the received message in a message box, showcasing a practical integration between legacy and modern programming environments.

Check the repository for the full source code

https://github.com/nikosportolos/COM-Interoperability-in-.NET/tree/main/part-ii

Table of Contents


.NET class library

Setup .NET project

  1. Create a new C# Project in Visual Studio

    dotnet_01
  2. Select Class Library

    dotnet_02
  3. Make assembly COM Visible

  • Right-Click on Project

  • Properties (Alt+Enter)

    dotnet_03
    • Application tab
    • Assembly Information button
    dotnet_04
    • Check option “Make assembly COM Visible”
    dotnet_05
  1. Register for COM Interop

    • Build tab
    • Output section
    • Check option “Register for COM interop”
    dotnet_06

C# source code

  1. Create your Event interface
[Guid("123456789-1234-1234-1234-123456789123")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IVB6InteropEvents
{
    [DispId(1)]
    void SampleEvent(string Message);
}
  1. Implement Events

    • Create delegate

      public delegate void SampleEventHandler(string Message);
    • Create event

      public new event SampleEventHandler SampleEvent = null;
    • Create event trigger

      public void FireSampleEvent(string Message);
      
      [Guid("7E6D6368-0033-49F6-9FE3-B2D409572869")]
      [ProgId("VB6Interop")]
      [ClassInterface(ClassInterfaceType.None)]
      [ComSourceInterfaces(typeof(IVB6InteropEvents))]
      [ComVisible(true)]
      public class VB6Interop : IVB6Interop
      {
        public void SampleMethod(string Message)
        {
            try
            {
                //MessageBox.Show(Message);
                FireSampleEvent("I received the message: " + Message);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception occured in SampleMethod(): ", ex);
            }
        }
        
        // Create delegate
        [ComVisible(true)]
        public delegate void SampleEventHandler(string Message);
        
        // Create event
        public new event SampleEventHandler SampleEvent = null;
        
        // Create event trigger
        public void FireSampleEvent(string Message)
        {
            try
            {
                if (SampleEvent != null)
                    SampleEvent(Message);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception occured in FireSampleEvent(): ", ex);
            }
        }
      }

VB6 winform app

Setup VB6 project

  1. Create a new VB6 Project
  • Select Standard EXE
vb_01
  • Add VB6Interop.dll reference
    • Project

    • References

      vb_02
    • Check VB6Interop Reference

      vb_03

VB6 source code

Option Explicit

' Declare a VB6Interop object
Public WithEvents VB6 As VB6Interop.VB6Interop
 
Private Sub Command1_Click()
    ' Use SampleMethod() of VB6Interop
    VB6.SampleMethod "Hello VB6!"
End Sub
 
Private Sub Form_Load()
    ' Initialize VB6Interop object
    Set VB6 = New VB6Interop.VB6Interop
End Sub
 
' Implement SampleEvent
Private Sub VB6_SampleEvent(ByVal Message As String)
    MsgBox Message
End Sub

Demo

vb_05

You may also be interested in