Hey all i found this code here in C# that allows the use of SendMessage API to send messages back and forth from 2 .net applications running. Everything goes well in the C# code when testing but i needed to convert it to VB.net.
So i used an online C#->VB.net converter and got this:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private Const RF_TESTMESSAGE As Integer = &HA123
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function SendMessage(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
Public Sub New()
InitializeComponent()
Me.Text = String.Format("This process ID: {0}", Process.GetCurrentProcess().Id)
End Sub
<STAThread()> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim proc As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(proc.ProcessName)
If processes.Length > 1 Then
For Each p As Process In processes
If p.Id <> proc.Id Then
SendMessage(p.MainWindowHandle, RF_TESTMESSAGE, IntPtr.Zero, IntPtr.Zero)
End If
Next
Else
MessageBox.Show("No other running applications found.")
End If
End Sub
Protected Overrides Sub WndProc(ByRef message As Message)
If message.Msg = RF_TESTMESSAGE Then
ListBox1.Items.Add("Received message RF_TESTMESSAGE")
End If
MyBase.WndProc(message)
End Sub
End Class
However, using the code above for both my apps doesnt seem to produce any sendmessage action when it gets to the If processes.Length > 1 Then. It always tells me No other running applications found. even though i have both apps running as i did in the C# example.
I must be missing something that didnt carry over when converting. Any help would be great to fix this!