Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / Visual Basic

VB.NET, VB6 and C# Interprocess communication via Window Messaging

Rate me:
Please Sign up or sign in to vote.
4.79/5 (18 votes)
16 May 20042 min read 235.1K   5.8K   61  
Article outlining methods for communicating between VB6, VB.NET, and C# apps using Window Messaging.
Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Declarations "
    Private mWindowMessagingClass As clsWindowMessaging
#End Region

#Region " Form and Object Event Handlers "

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mWindowMessagingClass = New clsWindowMessaging

        Me.DisplayDebugInfo()
    End Sub

    Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Me.mWindowMessagingClass.Dispose()
    End Sub

    Private Sub btnRefreshDebugInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshDebugInfo.Click
        Me.DisplayDebugInfo()
    End Sub

    Private Sub btnMessageToVB6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessageToVB6.Click
        Me.mWindowMessagingClass.SendMessageToVB6()
        Me.DisplayDebugInfo()
    End Sub

    Private Sub btnMessageToCSHARP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessageToCSHARP.Click
        Me.mWindowMessagingClass.SendMessageToCSHARP()
        Me.DisplayDebugInfo()
    End Sub

#End Region

#Region " Private Support Functions "

    Private Function DisplayDebugInfo()
        Me.lblMyHandleValue.Text = Me.Handle.ToString
        Me.lblVB6HandleValue.Text = Me.mWindowMessagingClass.VB6_WindowHandle.ToString
        Me.lblVBNETTOVB6MessageIdValue.Text = Me.mWindowMessagingClass.VB6_TO_VBNET_MessageId.ToString
        Me.lblVBNETTOCSHARPMessageIdValue.Text = Me.mWindowMessagingClass.VBNET_TO_CSHARP_MessageId.ToString()
        Me.lblCSHARPTOVBNETMessageIdValue.Text = Me.mWindowMessagingClass.CSHARP_TO_VBNET_MessageId.ToString()
        Me.lblVB6MessageIdValue.Text = Me.mWindowMessagingClass.VBNET_TO_VB6_MessageId.ToString
    End Function

#End Region

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
Friend WithEvents btnMessageToVB6 As System.Windows.Forms.Button
Friend WithEvents groupDebugInfo As System.Windows.Forms.GroupBox
Friend WithEvents lblMyHandle As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents lblVB6HandleValue As System.Windows.Forms.Label
Friend WithEvents lblVB6MessageIdValue As System.Windows.Forms.Label
Friend WithEvents lblMyHandleValue As System.Windows.Forms.Label
Friend WithEvents btnRefreshDebugInfo As System.Windows.Forms.Button
Friend WithEvents btnMessageToCSHARP As System.Windows.Forms.Button
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents lblVBNETTOVB6MessageIdValue As System.Windows.Forms.Label
Friend WithEvents lblVBNETTOCSHARPMessageIdValue As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents lblCSHARPTOVBNETMessageIdValue As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnMessageToVB6 = New System.Windows.Forms.Button
Me.groupDebugInfo = New System.Windows.Forms.GroupBox
Me.lblVB6HandleValue = New System.Windows.Forms.Label
Me.lblVB6MessageIdValue = New System.Windows.Forms.Label
Me.lblVBNETTOVB6MessageIdValue = New System.Windows.Forms.Label
Me.lblMyHandleValue = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.lblMyHandle = New System.Windows.Forms.Label
Me.btnRefreshDebugInfo = New System.Windows.Forms.Button
Me.btnMessageToCSHARP = New System.Windows.Forms.Button
Me.lblCSHARPTOVBNETMessageIdValue = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.lblVBNETTOCSHARPMessageIdValue = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.groupDebugInfo.SuspendLayout()
Me.SuspendLayout()
'
'btnMessageToVB6
'
Me.btnMessageToVB6.Location = New System.Drawing.Point(160, 208)
Me.btnMessageToVB6.Name = "btnMessageToVB6"
Me.btnMessageToVB6.Size = New System.Drawing.Size(120, 24)
Me.btnMessageToVB6.TabIndex = 0
Me.btnMessageToVB6.Text = "Message to VB6"
'
'groupDebugInfo
'
Me.groupDebugInfo.Controls.Add(Me.lblVBNETTOCSHARPMessageIdValue)
Me.groupDebugInfo.Controls.Add(Me.Label6)
Me.groupDebugInfo.Controls.Add(Me.lblCSHARPTOVBNETMessageIdValue)
Me.groupDebugInfo.Controls.Add(Me.Label5)
Me.groupDebugInfo.Controls.Add(Me.lblVB6HandleValue)
Me.groupDebugInfo.Controls.Add(Me.lblVB6MessageIdValue)
Me.groupDebugInfo.Controls.Add(Me.lblVBNETTOVB6MessageIdValue)
Me.groupDebugInfo.Controls.Add(Me.lblMyHandleValue)
Me.groupDebugInfo.Controls.Add(Me.Label3)
Me.groupDebugInfo.Controls.Add(Me.Label2)
Me.groupDebugInfo.Controls.Add(Me.Label1)
Me.groupDebugInfo.Controls.Add(Me.lblMyHandle)
Me.groupDebugInfo.Location = New System.Drawing.Point(8, 8)
Me.groupDebugInfo.Name = "groupDebugInfo"
Me.groupDebugInfo.Size = New System.Drawing.Size(432, 192)
Me.groupDebugInfo.TabIndex = 1
Me.groupDebugInfo.TabStop = False
Me.groupDebugInfo.Text = "Debug Info"
'
'lblVB6HandleValue
'
Me.lblVB6HandleValue.Location = New System.Drawing.Point(136, 50)
Me.lblVB6HandleValue.Name = "lblVB6HandleValue"
Me.lblVB6HandleValue.Size = New System.Drawing.Size(112, 16)
Me.lblVB6HandleValue.TabIndex = 7
Me.lblVB6HandleValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lblVB6MessageIdValue
'
Me.lblVB6MessageIdValue.Location = New System.Drawing.Point(136, 160)
Me.lblVB6MessageIdValue.Name = "lblVB6MessageIdValue"
Me.lblVB6MessageIdValue.Size = New System.Drawing.Size(112, 16)
Me.lblVB6MessageIdValue.TabIndex = 6
Me.lblVB6MessageIdValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lblVBNETTOVB6MessageIdValue
'
Me.lblVBNETTOVB6MessageIdValue.Location = New System.Drawing.Point(136, 79)
Me.lblVBNETTOVB6MessageIdValue.Name = "lblVBNETTOVB6MessageIdValue"
Me.lblVBNETTOVB6MessageIdValue.Size = New System.Drawing.Size(112, 16)
Me.lblVBNETTOVB6MessageIdValue.TabIndex = 5
Me.lblVBNETTOVB6MessageIdValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lblMyHandleValue
'
Me.lblMyHandleValue.Location = New System.Drawing.Point(136, 21)
Me.lblMyHandleValue.Name = "lblMyHandleValue"
Me.lblMyHandleValue.Size = New System.Drawing.Size(112, 16)
Me.lblMyHandleValue.TabIndex = 4
Me.lblMyHandleValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(8, 53)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(112, 16)
Me.Label3.TabIndex = 3
Me.Label3.Text = "VB6 Handle"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 160)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(112, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "VB6 MessageId:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 82)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(112, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "VB.NET to VB6"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'lblMyHandle
'
Me.lblMyHandle.Location = New System.Drawing.Point(8, 24)
Me.lblMyHandle.Name = "lblMyHandle"
Me.lblMyHandle.Size = New System.Drawing.Size(112, 16)
Me.lblMyHandle.TabIndex = 0
Me.lblMyHandle.Text = "My Handle:"
Me.lblMyHandle.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'btnRefreshDebugInfo
'
Me.btnRefreshDebugInfo.Location = New System.Drawing.Point(32, 208)
Me.btnRefreshDebugInfo.Name = "btnRefreshDebugInfo"
Me.btnRefreshDebugInfo.Size = New System.Drawing.Size(120, 24)
Me.btnRefreshDebugInfo.TabIndex = 2
Me.btnRefreshDebugInfo.Text = "Refresh Debug Info"
'
'btnMessageToCSHARP
'
Me.btnMessageToCSHARP.Location = New System.Drawing.Point(296, 208)
Me.btnMessageToCSHARP.Name = "btnMessageToCSHARP"
Me.btnMessageToCSHARP.Size = New System.Drawing.Size(120, 24)
Me.btnMessageToCSHARP.TabIndex = 3
Me.btnMessageToCSHARP.Text = "Message to C#"
'
'lblCSHARPTOVBNETMessageIdValue
'
Me.lblCSHARPTOVBNETMessageIdValue.Location = New System.Drawing.Point(136, 136)
Me.lblCSHARPTOVBNETMessageIdValue.Name = "lblCSHARPTOVBNETMessageIdValue"
Me.lblCSHARPTOVBNETMessageIdValue.Size = New System.Drawing.Size(112, 16)
Me.lblCSHARPTOVBNETMessageIdValue.TabIndex = 9
Me.lblCSHARPTOVBNETMessageIdValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(8, 136)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(112, 16)
Me.Label5.TabIndex = 8
Me.Label5.Text = "C# to VB.NET"
Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'lblVBNETTOCSHARPMessageIdValue
'
Me.lblVBNETTOCSHARPMessageIdValue.Location = New System.Drawing.Point(136, 112)
Me.lblVBNETTOCSHARPMessageIdValue.Name = "lblVBNETTOCSHARPMessageIdValue"
Me.lblVBNETTOCSHARPMessageIdValue.Size = New System.Drawing.Size(112, 16)
Me.lblVBNETTOCSHARPMessageIdValue.TabIndex = 11
Me.lblVBNETTOCSHARPMessageIdValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(8, 112)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(112, 16)
Me.Label6.TabIndex = 10
Me.Label6.Text = "VB.NET to C#"
Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 245)
Me.Controls.Add(Me.btnMessageToCSHARP)
Me.Controls.Add(Me.btnRefreshDebugInfo)
Me.Controls.Add(Me.groupDebugInfo)
Me.Controls.Add(Me.btnMessageToVB6)
Me.Name = "MainForm"
Me.Text = "VB.NET Main Form"
Me.groupDebugInfo.ResumeLayout(False)
Me.ResumeLayout(False)

    End Sub

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
Located in sunny Brisbane Australia, was working for three years in Internet company (ASP website and Exchange 5.5 programming mainly), and have moved on to a bankruptcy and insolvency company.

Working on in-house VB6/SQL Server 2000 application, VB.NET/SQL 2000 apps, C#/SQL 2000 app's and company's ASP/SQL 2000 website (blah, ASP is so yucky now I've used ASP.NET).

Personally working on C# / GDI+ RPG game and some other websites soon to be released.

Comments and Discussions