Communicate between two Visual Basic Applications

How to communicate between 2 different visual basic applications.

Simple technique by using SendMessage()

	'Main app that tries to communicate with other
	Public Class Form1
		'Project App1

		Private Const APP2_MSG1 As Integer = 6541

		Declare Auto Function SendMessage Lib "user32.dll" _
		(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
		 ByVal wParam As Integer, ByVal lParam As Integer) As Integer

		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
		Handles Button1.Click
			Dim p() As Process = Process.GetProcessesByName("App2")
			If p.Count > 0 Then
				Dim hWnd As IntPtr = p(0).MainWindowHandle
				SendMessage(hWnd, APP2_MSG1, 0, 0)
				Debug.WriteLine("Msg sent")
			End If
		End Sub
	End Class

Read more to get code of Listening application


	'App that listens to Main app
	Public Class Form1
		'Project App2
		'Final output App2.exe


		'You can declare number of constants and then do select case in WndProc
		Private Const APP2_MSG1 As Integer = 6541

		Private Sub someMethod()
			MsgBox("Message recieved")
			'Do your work here
		End Sub

		Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
			If m.Msg = APP2_MSG1 Then
				'call some method to do expect work
				someMethod()
				m.result = CType(0,IntPtr)
			Else
				MyBase.WndProc(m)
			End If
		End Sub

	End Class
Advertisements

2 comments

  1. FYI, the listening application must make sure the “Show In Taskbar” property of its main window form is set to “True” or the other app will not be able to communicate with it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s