none
Send string value between two applications RRS feed

  • Question

  • hi

    I want to send a text value from Textbox in app 1
    To any textbox, the focus is on another program app 2
    Where the two programs are in working condition

    Wednesday, November 20, 2019 12:39 PM

Answers

  • Hi,

    There are many ways to do this, such as:

    1) Establish Pipes between applications. Pipes are stream classes that can be connected between two applications.
    2) Use the TCP class to establish a virtual connection between applications.

    You can also view the following links:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/69526b52-bb7c-422f-a9bc-00d2a79d40d4/passing-data-between-applications-in-c?forum=csharpgeneral

    Because the result you need to implement is just to transfer the string, the method I use is to use FindWindow to get the handle of the app2 window and send the message using the SendMessage function.

    User32.dll is a Windows user interface related application interface for Windows processing, basic user interface and other features such as creating windows and sending messages. Communication between the two processes can be done by calling the api of user32.dll.

    The effect:

    app1:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Public Structure CopyDataStruct
                Public dwData As IntPtr
                Public cbData As Integer
                <MarshalAs(UnmanagedType.LPStr)>
                Public lpData As String
            End Structure
    
            Public Const WM_COPYDATA As Integer = &H4A
            <DllImport("User32.dll", EntryPoint:="FindWindow")>
            Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    
            End Function
            <DllImport("User32.dll", EntryPoint:="SendMessage")>
            Private Shared Function SendMessage(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As CopyDataStruct) As Integer
    
            End Function
    
    
    
            Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim strURL As String = TextBox1.Text
            Dim cds As CopyDataStruct
            cds.dwData = CType(1, IntPtr)
            cds.lpData = strURL
            cds.cbData = System.Text.Encoding.[Default].GetBytes(strURL).Length + 1
            SendMessage(FindWindow(Nothing, "app2"), WM_COPYDATA, 0, cds)
        End Sub
    End Class

    app2:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Structure CopyDataStruct
            Public dwData As IntPtr
            Public cbData As Integer
            <MarshalAs(UnmanagedType.LPStr)>
            Public lpData As String
        End Structure
    
        Private Const WM_COPYDATA As Integer = &H4A
    
        Protected Overrides Sub WndProc(ByRef e As System.Windows.Forms.Message)
            If e.Msg = WM_COPYDATA Then
                Dim cds As CopyDataStruct = CType(e.GetLParam(GetType(CopyDataStruct)), CopyDataStruct)
                TextBox1.Text = cds.lpData.ToString()
            End If
    
            MyBase.WndProc(e)
        End Sub
    End Class

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 21, 2019 5:19 AM
    Moderator

All replies


  • I want to send a text value from Textbox in app 1
    To any textbox, the focus is on another program app 2
    Where the two programs are in working condition

    See:

    Interprocess Communications
    https://docs.microsoft.com/en-us/windows/win32/ipc/interprocess-communications

    What is the simplest method of inter-process communication between 2 C# processes?
    https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro

    VB.NET IPC InterProcess Communication
    http://programmers-en.high-way.info/vb/ipc.html

    - Wayne

    Wednesday, November 20, 2019 1:09 PM
  • Hi,

    There are many ways to do this, such as:

    1) Establish Pipes between applications. Pipes are stream classes that can be connected between two applications.
    2) Use the TCP class to establish a virtual connection between applications.

    You can also view the following links:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/69526b52-bb7c-422f-a9bc-00d2a79d40d4/passing-data-between-applications-in-c?forum=csharpgeneral

    Because the result you need to implement is just to transfer the string, the method I use is to use FindWindow to get the handle of the app2 window and send the message using the SendMessage function.

    User32.dll is a Windows user interface related application interface for Windows processing, basic user interface and other features such as creating windows and sending messages. Communication between the two processes can be done by calling the api of user32.dll.

    The effect:

    app1:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Public Structure CopyDataStruct
                Public dwData As IntPtr
                Public cbData As Integer
                <MarshalAs(UnmanagedType.LPStr)>
                Public lpData As String
            End Structure
    
            Public Const WM_COPYDATA As Integer = &H4A
            <DllImport("User32.dll", EntryPoint:="FindWindow")>
            Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    
            End Function
            <DllImport("User32.dll", EntryPoint:="SendMessage")>
            Private Shared Function SendMessage(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As CopyDataStruct) As Integer
    
            End Function
    
    
    
            Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim strURL As String = TextBox1.Text
            Dim cds As CopyDataStruct
            cds.dwData = CType(1, IntPtr)
            cds.lpData = strURL
            cds.cbData = System.Text.Encoding.[Default].GetBytes(strURL).Length + 1
            SendMessage(FindWindow(Nothing, "app2"), WM_COPYDATA, 0, cds)
        End Sub
    End Class

    app2:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Structure CopyDataStruct
            Public dwData As IntPtr
            Public cbData As Integer
            <MarshalAs(UnmanagedType.LPStr)>
            Public lpData As String
        End Structure
    
        Private Const WM_COPYDATA As Integer = &H4A
    
        Protected Overrides Sub WndProc(ByRef e As System.Windows.Forms.Message)
            If e.Msg = WM_COPYDATA Then
                Dim cds As CopyDataStruct = CType(e.GetLParam(GetType(CopyDataStruct)), CopyDataStruct)
                TextBox1.Text = cds.lpData.ToString()
            End If
    
            MyBase.WndProc(e)
        End Sub
    End Class

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 21, 2019 5:19 AM
    Moderator
  • If the focus is already on target textbox, then, in simple cases, you can also use SendKeys.Send to paste some elementary texts.

    Thursday, November 21, 2019 6:05 AM
  • I want to send a text value from Textbox in app 1

    To any textbox, the focus is on another program app 2
    Where the two programs are in working condition

    Just send WM_SETTEXT
    Thursday, November 21, 2019 6:24 AM