0

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!

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • 1
    `both apps running` - two instances of the same program, or different programs? BTW, why don't you use WCF? – Alex F Jan 14 '14 at 15:49
  • 2
    Good Lord, learning how to write C# code is going to take you less time than getting that converted crap to run. Which is my recommendation, understanding C# is a pretty hard requirement for VB.NET programmers these days. – Hans Passant Jan 14 '14 at 16:35
  • The blog post link you provided doesn't work here. Maybe it's time to consider using a first-class inter-process communication method like WCF, or even Remoting. See also http://stackoverflow.com/questions/2082450/communication-between-c-sharp-applications-the-easy-way – Robert Harvey Jan 14 '14 at 16:39
  • @AlexFarber its different programs. Each running in its own IDE VS2010. In the example from the link i had the same setup and they both talked to each other just fine. – StealthRT Jan 14 '14 at 19:45
  • @RobertHarvey The post does work... Maybe it was having problems at the time you tried to visit it? My code is already in Win form so going to WCF is not an option right now. – StealthRT Jan 14 '14 at 19:46
  • SendMessage would not be my first choice. Are you using SendMessage because the target program is not yours? – Robert Harvey Jan 14 '14 at 19:47
  • @RobertHarvey No, i have code for both - just seemed like an easy setup (at least in the C# demo) in order to send data to an app and vis-versa. – StealthRT Jan 14 '14 at 20:33
  • It's very brittle. Spend the few minutes and learn how to do it with WCF or Remoting. – Robert Harvey Jan 14 '14 at 20:38
  • @RobertHarvey I found this [ http://www.thecodeking.co.uk/2009/12/xdmessaging-net-library-20.html#.UtWjwIakpWY ] called **XDMessaging** but i am unable to get it working as well. That seems pretty stright forward as well if only i could get it working. – StealthRT Jan 14 '14 at 20:54
  • This code works for instances of the same program. For different programs you need another logic. `Process.GetProcessesByName(proc.ProcessName)` gets processes with the same name as current process. You need to change this by searching for another process name. – Alex F Jan 15 '14 at 08:22
  • @AlexFarber that doesnt make sence? I run 2 IDEs with the C# code and they communicate just fine. One has a number of 5468 while the other 9102. The same goes for the VB version i converted... but it doesnt work the same as it does in C# it seems. – StealthRT Jan 15 '14 at 15:00

1 Answers1

0

The fact that you are getting the "No other running applications found" message indicates a problem in this bit of code. Check out my notes here:

    Dim proc As Process = Process.GetCurrentProcess()
    //This gets an array of all processes currently running with the same name
    //  as this application.  
    Dim processes As Process() = Process.GetProcessesByName(proc.ProcessName)

    //If there is only one process with this application's name, then it's
    //  THIS process, so there aren't any others by the same name running
    //  right now.  
    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
        //Thus, this gets called because there are not other applications with 
        //the same name.
        MessageBox.Show("No other running applications found.")
    End If

Please be aware that when you run code in the environement, modern studio version usually show up as DevEnv.exe, so running two instances of the IDE should normally work. However! There is an option in Visual Studio to run the code under its own application (executable) name. If you go to your Project Properties --> Debug, there is an option labeled Enable the Visual Studio hosting process. Disabling that checkbox will cause the code to run under the name of its compiled executable.

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52