Member Avatar for Huntondoom

hey, for an Application I want to let application Communicate with each other but how would that be done?
I know of Commandlinearguments but that is only when starting up.
I want them to communicate while they are running.
how would I do such a thing?

Recommended Answers

All 4 Replies

Member Avatar for Unhnd_Exception

You can use the winapi SendMessage.

This example will send a simple message of 1 to another window.

Don't know what your trying to accomplish but maybe this will help.

'A project named WindowToSendFrom

Imports System.Runtime.InteropServices

Public Class Form1
    'WM_USER     
    Private Const WM_USER As Integer = 1024

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Get the handle to the window named Window To Send to
        Dim WindowHandle As IntPtr = FindWindow(Nothing, "Window To Send To")

        'Send a message to the window
        Dim ReturnMessage As IntPtr = SendMessage(WindowHandle, WM_USER, 1, 0)
    End Sub

    <DllImport("User32.dll")> _
    Public Shared Function FindWindow( _
       ByVal lpClassName As String, _
       ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("User32")> _
    Public Shared Function SendMessage(ByVal Handle As IntPtr, _
                                  ByVal msg As Integer, _
                                  ByVal wParam As IntPtr, _
                                  ByVal lParam As Integer) As IntPtr
    End Function

End Class

'an entirely different solution named WindowToSendTo

'The text of the form1 is set to Window To Send To

'Receives the wm_user message from the other window
'if the wparam is 1 it sets the label to 1

'other things could be passed in this is just a simple example of receiving a message from another window.

Public Class Form1

    Private Const WM_USER As Integer = 1024
    Private fpCommand As Integer

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_USER Then
            fpCommand = CInt(m.WParam)

           if fpcommand = 1 then Label1.Text = fpCommand
        End If
        MyBase.WndProc(m)

    End Sub

End Class
Member Avatar for Huntondoom

great reply has ever Unhnd Exception

Member Avatar for Huntondoom

hey just saw it but is it possible to send a string or ? :S

Member Avatar for Unhnd_Exception

you'll have to send the wm_copydata message and a copydata structure

heres a modified version of the first that should send your string data.

'the window to send from

Imports System.Runtime.InteropServices

Public Class Form1

    Private Const WM_CopyData As Integer = 74

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim WindowHandle As IntPtr = FindWindow(Nothing, "Window To Send To")
        Dim StringToSend As String = "Command1;Command2;Command3"
        Dim DataStructure As New CopyDataStruct

        DataStructure.dwData = IntPtr.Zero
        DataStructure.cbData = StringToSend.Length * 2
        DataStructure.lpData = StringToSend

        SendMessage(WindowHandle, WM_CopyData, Me.Handle, DataStructure)

    End Sub

    <DllImport("User32.dll")> _
    Public Shared Function FindWindow( _
       ByVal lpClassName As String, _
       ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("User32")> _
    Public Shared Function SendMessage(ByVal Handle As IntPtr, _
                                  ByVal msg As Integer, _
                                  ByVal wParam As IntPtr, _
                                  ByRef lParam As CopyDataStruct) As IntPtr
    End Function

End Class

<StructLayout(LayoutKind.Sequential)> _
   Public Structure CopyDataStruct
    Public dwData As IntPtr
    Public cbData As Integer
    <MarshalAs(UnmanagedType.LPStr)> _
    Public lpData As String
End Structure

'The window sending to. Added a reference to the WindowToSendFrom app to get the structure.

Imports System.Runtime.InteropServices

Public Class Form1

    Private Const WM_CopyData As Integer = 74

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = WM_CopyData Then

            Dim DataSentIn As WindowToSendFrom.CopyDataStruct = CType(Marshal.PtrToStructure(m.LParam, GetType(WindowToSendFrom.CopyDataStruct)), WindowToSendFrom.CopyDataStruct)
            Dim Commands As String() = DataSentIn.lpData.Split(New Char() {";"c})

            Label1.Text = String.Empty
            For i = 0 To UBound(Commands)
                Label1.Text &= Commands(i) & vbCrLf
            Next

        End If

        MyBase.WndProc(m)

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.