Results 1 to 20 of 20

Thread: [RESOLVED] How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Resolved [RESOLVED] How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Program

    I want to make a program that can pass the value of text box to another text box of open program.

    I search in the internet and found this code.
    this will pass the value of text box to notepad when click the button, because i specify the file name of the notepad.
    I try to change the name of notepad to the name of the program that will receive the data but didn't work.

    Code:
    Imports System.Runtime.InteropServices 
    Public Class Form2 
        Private Const WM_SETTEXT = &HC 
        Private Declare Function SendMessageSTRING Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, _ 
       ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32 
     
        Private Declare Function FindWindow Lib "user32.dll" Alias _ 
    "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32 
        Private Declare Function FindWindowEx Lib "user32.dll" Alias _ 
      "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, _ 
      ByVal lpsz2 As String) As Int32 
     
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
      
            Dim Hwnd As IntPtr = FindWindow(Nothing, "Untitled - Notepad") 
            ' Dim Hwnd As IntPtr = FindWindow(" ProgramName", Nothing) 
     
            Dim Handle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing) 
            SendMessageSTRING(Handle, WM_SETTEXT, TextBox1.TextLength, TextBox1.Text) 
     
        End Sub 
    End Class


    Please help me modify my code :
    1. If i click Button in my one program the data will be pass to the other program that has text box also and where the index / cursor is focus to it.not in notepad.

    thanks in advanced.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    I try to change the name of notepad to the name of the program that will receive the data
    Um, no you didn't. The FindWindow API function takes either the class name of the window, the window title or both. In this line:
    vb.net Code:
    1. Dim Hwnd As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
    you are passing the title only. If you expect to find another window by title then you have todo it the same way, i.e. pass a value for the title. In this line:
    vb.net Code:
    1. Dim Hwnd As IntPtr = FindWindow(" ProgramName", Nothing)
    you're passing a value for the class name and not for the window title. Did you pass a valid class name? If not, why would you expect it to work? If the value you have a is a title then pass it to the title parameter, not the class name parameter.

    Also, how you use FindWindowEx to get the handle of the child window of interest depends on the structure of the main window. This line:
    vb.net Code:
    1. Dim Handle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
    may work for Notepad but if the application window you're trying to affect has a different structure then it won't work. Have you actually used a tool like Spy++ to determine what the structure of that window is?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Quote Originally Posted by jmcilhinney View Post
    Um, no you didn't. The FindWindow API function takes either the class name of the window, the window title or both. In this line:
    vb.net Code:
    1. Dim Hwnd As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
    you are passing the title only. If you expect to find another window by title then you have todo it the same way, i.e. pass a value for the title. In this line:
    vb.net Code:
    1. Dim Hwnd As IntPtr = FindWindow(" ProgramName", Nothing)
    you're passing a value for the class name and not for the window title. Did you pass a valid class name? If not, why would you expect it to work? If the value you have a is a title then pass it to the title parameter, not the class name parameter.

    Also, how you use FindWindowEx to get the handle of the child window of interest depends on the structure of the main window. This line:
    vb.net Code:
    1. Dim Handle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
    may work for Notepad but if the application window you're trying to affect has a different structure then it won't work. Have you actually used a tool like Spy++ to determine what the structure of that window is?

    Hi Sir,

    Thank you for the reply,

    Now I modify my code base on the data I got using spy++ but still not work please advice.
    thank you.

    I use the TEXT or Title using SPY++ data.

    Code:
    Dim Hwnd As IntPtr = FindWindow("CAPTURE", Nothing)
    then I use the class by selecting the text box of another program where i want to pass the data,
    but still not pass the data. please advice I am new to vb.net.

    Code:
    Dim Handle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.141b42a_r15_ad1", Nothing)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Did FindWindow get a valid handle?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    how will i know if it get valid handle ? i set my FindWindowEx to the class name of the textbox i knew the name using spy++.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Quote Originally Posted by BONITO View Post
    how will i know if it get valid handle ?
    Look at the value returned by the method. Does it match what Spy++ is telling you?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    I try code below but didn't work ,any advice?

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim Hwnd As Integer = Win32.FindWindow("WindowsForms10.Window.8.app.0.141b42a_r15_ad1", "Capture") 
    Dim CHwnd As Integer = Win32.FindWindowEx(Hwnd, Nothing, "WindowsForms10.EDIT.app.0.141b42a_r15_ad1", Nothing) 
     
            If (Not Hwnd = 0 And Not CHwnd = 0) Then 
                Win32.SetForegroundWindow(CHwnd) 
                Win32.SendMessage(CHwnd, Win32.WM_SETTEXT, TextBox1.TextLength, TextBox1.Text) 
                Else 
                MessageBox.Show("Handler not found,check the title") 
     
            End If 
        End Sub
    but when i target notepad it can pass normal.

    Code:
    Dim Hwnd As Integer = Win32.FindWindow(Nothing, "Untitled - Notepad") 
            Dim CHwnd As Integer = Win32.FindWindowEx(Hwnd, Nothing, "Edit", Nothing)
    below is my Class in Win32
    Code:
    Public Class Win32 
    #Region "Methods and functions declaration" 
        Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32 
        Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32 
        Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32 
        Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32 
        Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32 
     
    #Region "Messages" 
        Public Const WM_SETTEXT As Int32 = &HC 
    #End Region 
     
    #End Region 
    End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    So do FindWindow and FindWindowEx return the handles you expect, i.e. the ones that Spy++ indicates?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    I I knew now my problem , I must use EnumChildWindows Function , because the textbox that I am targeting is at the long list under the Parent Handler. can anyone modify my code use EnumchildWindows and EnumWindowsProc ? or any good link ? thanks in advance .

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    You don't need to call EnumChildWindows. You just need to call FindWindowEx multiple times. For instance, if you want to get the third child of that type, you need to call FindWindowEx three times. The second parameter is the handle of the sibling control before the control you want. The call you are currently making is going to get the first child of that type. If you make a second call and pass the result of the first as sibling then you'll get the second child of that type, then pass that result to get the third:
    vb.net Code:
    1. Dim Hwnd As Integer = Win32.FindWindow("WindowsForms10.Window.8.app.0.141b42a_r15_ad1", "Capture")
    2. Dim childWindowTypeName = "WindowsForms10.EDIT.app.0.141b42a_r15_ad1"
    3.  
    4. 'Get the first child of the type
    5. Dim CHwnd As Integer = Win32.FindWindowEx(Hwnd, Nothing, childWindowTypeName, Nothing)
    6.  
    7. 'Get the second child of the type
    8. CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, childWindowTypeName, Nothing)
    9.  
    10. 'Get the third child of the type
    11. CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, childWindowTypeName, Nothing)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    hi Sir,
    I am new to vb.net, I am a bit confuse what to use inside the parameter of finwindowex,
    example see image below the target control that i want to access is the last item, it is a textbox.
    how can i set the findwindowEx parameter to find the last item ?
    Name:  sssssss.PNG
Views: 1094
Size:  68.8 KB

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    hi Please read my comments on my code I am stock on getting the handler of the 2nd child. please advice thanks.
    Code:
    'parameter className and Title of Parent control 
            Dim Hwnd As Integer = Win32.FindWindow(Nothing, "Form1") 
            'you declare new variable , what value should i put to it ? the classname of last child that i want to target ???? or the first child ?  
            ' Dim ChildWindowTypeName = "WindowsForms10.EDIT.app.0.af25cb_r31_ad1" 
            '3rd parameter is the class name of the 1st child  
            Dim CHwnd As Integer = Win32.FindWindowEx(Hwnd, Nothing, "WindowsForms10.Window.8.app.0.af25cb_r31_ad1", Nothing) 
            '3rd parameter is the class name of the 2nd child 
            CHwnd = Win32.FindWindowEx(Hwnd, Nothing, "WindowsForms10.BUTTON.app.0.af25cb_r31_ad1", Nothing)

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    You use FindWindow to get the handle of a top-level window. You use FindWindowEx to get the handle of a child window, whether or not that window is a child of a top-level window or another child. In your case, the top-level window has a type of "WindowsForms10.Window.8.app.0.af25cb_r31_ad1" and a title of "Form1".

    You need to call FindWindow and specify those two values to get the handle 0007082C.
    You then need to call FindWindowEx and specify that top-level window as the parent, looking for a child window with a type of "WindowsForms10.Window.8.app.0.af25cb_r31_ad1" and a title of "DETAILS" to get the handle 0012072E.
    You then need to call FindWindowEx again and specify that first child handle as the parent, looking for a child window with a type of "WindowsForms10.EDIT.app.0.af25cb_r31_ad1" to get the handle 001607DC.

  14. #14
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Quote Originally Posted by BONITO View Post
    hi Please read my comments on my code I am stock on getting the handler of the 2nd child. please advice thanks.
    Try something like this, NOTE I did this in VB10/FW 4.0 so be sure to change the classname for the textbox (WindowsForms10.EDIT...).

    Code:
    Imports System.Runtime.InteropServices
    Public Class FrmMain
    
    Private Const WM_SETTEXT As Integer = &HC
    
    <DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As IntPtr, <MarshalAs(UnmanagedType.LPStr)> ByVal lParam As String) As IntPtr
    End Function
    
    <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' get main window  
        Dim hWnd1 As IntPtr = FindWindow(Nothing, "Form1")
        ' get first window with text "DETAILS" in the window
        Dim hWnd2 As IntPtr = FindWindowEx(hWnd1, IntPtr.Zero, Nothing, "DETAILS")
        ' classname for .net editbox
        Dim editboxClassName = "WindowsForms10.EDIT.app.0.141b42a_r14_ad1"
        ' get first Edit control in the window
        Dim hWnd3 As IntPtr = FindWindowEx(hWnd2, IntPtr.Zero, editboxClassName, Nothing)
        ' Send text to the edit control
        SendMessage(hWnd3, WM_SETTEXT, IntPtr.Zero, "Hello World!")
        ' Example to get 2nd 'Edit' control in the window
        ' Dim hWnd4 As IntPtr = FindWindowEx(hWnd2, hWnd3, editboxClassName, Nothing)
    End Sub
    
    End Class
    EDIT: Also note that classnames in .Net can change when the .Net framework(s) are updated (and debug mode) , so your app may break unexpectedly. One way around that might be to enumerate the child windows looking for say windows with "EDIT" in their classname. Best of luck!
    Last edited by Edgemeal; Jun 18th, 2018 at 01:43 PM. Reason: Add WM_SETTEXT

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Hi Sir,
    Thank you I got it in few child window,

    but when i try it to many child window i able to get the handler but the data i want to pass is not passing to the last control i set.


    Name:  sx.PNG
Views: 1125
Size:  381.2 KB
    my code below.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     
    'Parent 
            Dim Hwnd As Integer = Win32.FindWindow(nothing,"Test") 
            '1st 
            Dim CHwnd As Integer = Win32.FindWindowEx(Hwnd, Nothing, "ui60Drawn_W32", Nothing) 
            '2nd 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "ui60Viewcore_W32", Nothing) 
            '3rd 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "ui60Drawn_W32", Nothing) 
            '4th 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "MDIClient", Nothing) 
            '5th 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "ui60MDIdocument_W32", Nothing) 
            '6th 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "ui60Viewcore_W32", Nothing) 
            '7th 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "ui60Drawn_W32", Nothing) 
            '8th the final control i want to pass my data, but the data was not pass, can only get the handler. 
            CHwnd = Win32.FindWindowEx(Hwnd, CHwnd, "Edit", Nothing) 
     
    If (Not Hwnd = 0 And Not CHwnd = 0) Then 
                Win32.SendMessage(CHwnd, Win32.WM_SETTEXT, TextBox1.TextLength, TextBox1.Text) 
                MessageBox.Show(Hwnd) 
            Else 
                MessageBox.Show("Handler not found,check the title") 
     
            End If 
        End Sub

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    You really are not getting the principle here. Your first three calls to FindWindowEx are pointless. You only need to make one call to FindWindowEx to get the window of type MDIClient because it is the first of that type.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    It goes like this:

    FindWindow to get 0002082A
    FindWindowEx as child of 0002082A to get 00020828
    FindWindowEx as child of 00020828 to get 001306B6
    FindWindowEx as child of 001306B6 to get 00180846
    FindWindowEx as child of 00180846 to get 001406BC
    FindWindowEx as child of 001406BC to get 000507C2
    FindWindowEx as child of 001406BC and sibling of 000507C2 to get 000507E4
    FindWindowEx as child of 001406BC and sibling of 000507E4 to get 000607DA
    FindWindowEx as child of 000607DA to get 0020068A

    and then SendMessage.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Let me see whether I can clarify the principle that you're missing. What FindWindow and FindWindowEx do is basically get a list of windows, filter it, order it by z-index, and take the first one. FindWindow works for top-level windows and allows you to filter by window name and/or class name. FindWindowEx works for child windows and allows you to filter by parent window, prior sibling window, window name and/or class name.

    When using Spy++, you should basically work up the tree from the child window you want to the top-level window it's contained in.

    1. You want handle 0020068A, which has a class name "Edit" and is the first of its type in parent 000607DA.
    2. You want handle 000607DA, which has a class name "ui60Drawn_W32" and is the third of its type after sibling 000507E4 in parent 001406BC.
    3. You want handle 000507E4, which has a class name "ui60Drawn_W32" and is the second of its type after sibling 000507C2 in parent 001406BC.
    4. You want handle 000507C2, which has a class name "ui60Drawn_W32" and is the first of its type in parent 001406BC.
    5. You want handle 001406BC, which has a class name "ui60Viewcore_W32" and is the first of its type in parent 00180846.
    6. You want handle 00180846, which has a class name "ui60Drawn_W32" and is the first of its type in parent 001306B6.
    7. You want handle 001306B6, which has a class name "ui60MDIdocument_W32" and window name "Test" and is the first of its type in parent 00020828.
    8. You want handle 00020828, which has a class name "MDIClient" and is the first of its type in parent 0002082A.
    9. You want handle 0002082A, which is a top-level window with a class name "ui60MDIroot_W32" and a window name "Test".

    That's the sequence of handles you need to get so start at the bottom with a FindWindow call and then call FindWindowEx for each of the others.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    90

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    hi Sir,

    Thank you very much for the explanation now i got it.
    how can i mark this thread as Resolved ? i'm new using this.
    thanks.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How do I use WM_GETTEXT in VB.net to Pass text from 1 Progrm to another open Prog

    Quote Originally Posted by BONITO View Post
    how can i mark this thread as Resolved ?
    Use the Thread Tools menu near the top of the page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width