Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Visual Basic
Article

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 234.3K   5.8K   61   34
Article outlining methods for communicating between VB6, VB.NET, and C# apps using Window Messaging.

Introduction

There are quite a few articles on inter-process communication for VB6, and quite a few articles for inter-process communication in .NET. But it is an interesting mix using managed and unmanaged environments for inter-process communication. I have finally got it working after some issues with VB6. :-)

Background

My company is currently developing new applications in .NET, but we still have to support our (main company app) legacy VB6 applications. We had started to use COM-interop from VB6 to VB.NET components, but struck some bugs trying to automate applications from each other. Having a look around, I figured that Window Messaging was independent of VB and .NET environments. So then began my search of code samples and explanations of doing this. Some explained VB6 messaging, some explained .NET messaging, but couldn't find any that did both.

Using the code

For an explanation on the VB.NET sub-classing code, refer to this CodeProject article. I have taken the code straight from here and see no point in explaining it again.

VB6: Form interface:

VB
Private Sub Form_Load()
  ....
  'Create custom window and start listening to window messages.
  modMessaging.InitWindowMessaging
End Sub

Private Sub Form_Unload(Cancel As Integer)
  'Tear down custom message handling and pass
  'back to original message handler i.e this form
  modMessaging.StopWindowMessaging
End Sub

VB6: Messaging

VB
' Function to create custom window and setup Message Listening
Public Function InitWindowMessaging()

  '\\This statement creates the new window
  hWindowHandle = CreateWindowEx(0, "STATIC", VB6_WINDOWTITLE_SERVER, _
    0, 0, 0, 0, 0, 0, 0, App.hInstance, ByVal 0&)

  '\\ This statement sets the message handling to the
  '\\ProcessWindowMessages function defined later.
  ' We also save the address (hOldProc) of
  '\\of the previous MessageHandler so we
  ' can reset on StopWindowMessaging>
  hOldProc = SetWindowLongApi(hWindowHandle,_
     GWL_WNDPROC, AddressOf ProcessWindowMessages)

  WindowMessagingInitialised = True
End Function

'Function to tear down Message Handling
'and return to original Message Handler
Public Function StopWindowMessaging()
  '\\This statement sets the Message Handling
  'to be set to the address
  '\\of the previous MessageHandler which
  'we saved before changing it to ours.
  Call SetWindowLongApi(hWindowHandle, GWL_WNDPROC, hOldProc)
End Function

'Function to find VB.NET window and attempt to send message
Public Function SendMessageToVBNET()
  Dim hwndTarget As Long
  Dim MessageId As Long

  If WindowMessagingInitialised = False Then
    InitWindowMessaging
  End If

  'Get TargetWindow handle from global Window Name
  hwndTarget = VBNET_WindowHandle

  'Get MessageId from API call to RegisterMessage
  MessageId = VB6_TO_DOTNET_MessageId

  'If Window target exists, then PostMessage to target
  If
  hwndTarget <>  0 Then
      Call PostMessage(hwndTarget, MessageId, 0, 0)
  End If
End Function

Function to process messages. If not one of our custom messages,
'then fall through to original Message Handler
Private Function ProcessWindowMessages(ByVal hwnd As Long, _
  ByVal wMsg As Long, _
  ByVal wParam As Long, _
  ByVal lParam As Integer) As Long

  If wMsg = DOTNET_TO_VB6_MessageId Then
    '\\Respond to the custom message here
    MsgBox "window message received from DOTNET environment"
  Else
    '\\Pass the message to the previous
    'window procedure to handle it
    ProcessWindowMessages = CallWindowProc(hOldProc, _
                             hwnd, wMsg, wParam, lParam)
  End If

End Function

Points of Interest

One thing which I think is quite nice about this implementation is that it creates its own windows with user defined names. So you have global constants defined in VB6 and VB.NET for the window names. You can then rename your application, and the communication won't break.

The RegisterWindowMessage API call provides a system-wide MessageId. Subsequent calls to RegisterWindowMessage will return the same MessageId.

VB6: One thing to note that is vitally important is that you need to disconnect your custom Message Handler before the application closes, otherwise it starts to handle messages in the VB IDE. It was quite hard to track this problem down because it would work nicely when I ran the executable, but once inside the IDE, it would crash VB. Once I started writing debug.print statements in my Message Handler function, all became clear. Somehow, I was not handing message handling responsibilities to the MainForm.

VB.NET: Nothing much to note here. So much easier in .NET than VB. :-)

Links used while developing:

Version 3 now has VB6App2 source code. You can now use Window Messaging:

  • VB6 -> VB.NET
  • VB6 -> C#
  • VB6 -> VB6App2
  • VB6App2 -> VB6
  • VB.NET -> VB6
  • VB.NET -> C#
  • C# -> VB6
  • C# -> VB.NET

History

  1. Initial release to The Code Project
  2. Updated source and demos to include C# project.
  3. Updated source and demos to include VB6 to VB6App2.

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

 
QuestionSome VB6 projects that can help Pin
ISpliter3-Mar-14 13:45
ISpliter3-Mar-14 13:45 
QuestionBug Pin
Member 874123230-May-13 3:19
Member 874123230-May-13 3:19 
BugCritical bug in the code: Integer should be Long or VB6 app will crash Pin
Kenneth Choe20-Nov-12 5:37
Kenneth Choe20-Nov-12 5:37 
GeneralMy vote of 5 Pin
Member 43208444-May-11 16:45
Member 43208444-May-11 16:45 
NewsVery Important Note Pin
Grim4rt10-Feb-09 23:02
Grim4rt10-Feb-09 23:02 
GeneralRe: Very Important Note Pin
Member 1398105712-Aug-20 19:36
Member 1398105712-Aug-20 19:36 
GeneralDownload book for Interprocess Communication and Network Pin
AsirTim18-Jun-08 23:17
AsirTim18-Jun-08 23:17 
GeneralIn Vista Pin
chrishuff6-Jun-07 3:43
chrishuff6-Jun-07 3:43 
Generalpassing some data along with the windowMessage Pin
K edar V20-Jun-06 23:35
K edar V20-Jun-06 23:35 
GeneralRe: passing some data along with the windowMessage [modified] Pin
angus_grant21-Jun-06 1:07
angus_grant21-Jun-06 1:07 
GeneralRe: passing some data along with the windowMessage Pin
K edar V21-Jun-06 2:27
K edar V21-Jun-06 2:27 
GeneralRe: passing some data along with the windowMessage [modified] Pin
angus_grant21-Jun-06 17:46
angus_grant21-Jun-06 17:46 
GeneralRe: Gr8 work but.....It's(problem) is different Pin
K edar V30-Jun-06 19:46
K edar V30-Jun-06 19:46 
GeneralRe: Gr8 work but.....It's(problem) is different [modified] Pin
angus_grant30-Jun-06 21:33
angus_grant30-Jun-06 21:33 
GeneralPAssing parameters Pin
lynchbaby13-Apr-06 4:59
lynchbaby13-Apr-06 4:59 
how do you pass paramters as part of the windows message?
ex.I want to send a message from a VB app to a VB.NEt app but LOAD_CUSTOMER with a customer number 12345
GeneralRe: PAssing parameters Pin
angus_grant13-Apr-06 12:05
angus_grant13-Apr-06 12:05 
GeneralHWND_BROADCAST Pin
AndrewVos28-Mar-06 22:02
AndrewVos28-Mar-06 22:02 
GeneralVB6 to VB6messaging via LAN Pin
Salim Dallal22-Jan-06 21:16
Salim Dallal22-Jan-06 21:16 
GeneralRe: VB6 to VB6messaging via LAN Pin
angus_grant23-Jan-06 11:20
angus_grant23-Jan-06 11:20 
GeneralRe: VB6 to VB6messaging via LAN Pin
Salim Dallal23-Jan-06 20:07
Salim Dallal23-Jan-06 20:07 
QuestionPassing string as messages? Pin
Josef Meile23-May-05 7:44
Josef Meile23-May-05 7:44 
AnswerRe: Passing string as messages? Pin
Josef Meile23-May-05 7:58
Josef Meile23-May-05 7:58 
GeneralRe: Passing string as messages? Pin
Josef Meile23-May-05 11:37
Josef Meile23-May-05 11:37 
GeneralConfused... :) Pin
Icingdeath4-May-05 10:31
Icingdeath4-May-05 10:31 
GeneralRe: Confused... :) Pin
John Korres4-May-05 12:04
John Korres4-May-05 12:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.