Control another app (Spy++)

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2281 Views - Last Post: 12 September 2014 - 02:41 AM Rate Topic: -----

#1 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Control another app (Spy++)

Posted 10 April 2014 - 02:28 AM

Hello

I want to control another app by read value and send text but I don't know how to get what i need.
I need get text from (3) and set text at (4) as my photo.

Posted Image
I have written a progame as below.
Public Class Main

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Const BM_CLICK As Integer = &HF5
    Private Const WM_GETTEXT As Integer = &HD
    Private Const WM_SETTEXT As Long = &HC
    Private iHwndForm As IntPtr 
    Private iHwndChild As IntPtr 
    Private iHwndGETTEXT As IntPtr 
    Private iHwndSENTTEXT As IntPtr 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        iHwndForm = FindWindow(vbNullString, "Height Measurement")
        If iHwndForm <> 0 Then
            iHwndChild = FindWindowEx(iHwndForm, IntPtr.Zero, "Windows10.window.8.app.0.c99e5e_r13_ad1", Nothing)
            iHwndSETTEXT= FindWindowEx(iHwndChild, IntPtr.Zero, "Windows10.EDIT.app.0.c99e5e_r13_ad1", Nothing) ' Set text
            iHwndGETTEXT= FindWindowEx(iHwndChild, IntPtr.Zero, "Windows10.STATIC.app.0.c99e5e_r13_ad1", Nothing) ' GET text

            SendMessage(iHwndSETTEXT, WM_SETTEXT, 0, "Something") ' SENT "Something" to another app
            Dim txt As String = Space(txtlen + 1)
            SendMessage(iHwndGETTEXT, WM_GETTEXT, txtlen + 1, txt)
            TextBox1.Text = txt

        End If
End sub

End Class


but two line i'm not sure how to found them ?

            iHwndSETTEXT= FindWindowEx(iHwndChild, IntPtr.Zero, "Windows10.EDIT.app.0.c99e5e_r13_ad1", Nothing) ' Set text
            iHwndGETTEXT= FindWindowEx(iHwndChild, IntPtr.Zero, "Windows10.STATIC.app.0.c99e5e_r13_ad1", Nothing) ' GET text

This post has been edited by little_prince41: 10 April 2014 - 02:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Control another app (Spy++)

#2 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 10 April 2014 - 06:31 PM

Hi,
Unfortionatly all the controls made in VB like the app your trying to get the Label and TextBox from have screwed up class names that change every time the application is run so, they might work once for you but, as soon as you close the app and reopen it, it won`t work anymore.

The only way to find the controls in an app made with VB is to use a loop with the FindWindowEx API function and get the control at the correct index. That could be tricky though. If any controls are created, removed, or changed on the other app then that will throw the index numbers off.

For example, in the app your showing in Spy++, you would first find the parent window (1) and then you would use FindWindowEx in a loop to get the 6th control in the parent window which will be the child window (2). Then you would need to use FindWindowEx again in a loop to find the 15th control of the child window which would be the Label (3) and do the same to find the TextBox Edit window (4).

I can`t guarantee this will work the first time but, it should get you close if it doesn`t work the first time. You can try this in a new form project with 1 Button and 2 TextBoxes on it. When you press button1, TextBox1 will be set to the text that is retrieved from the Label on the other app and TextBox2 is the text that will be sent to the TextBox Edit window of the other app.
Imports System.Runtime.InteropServices

Public Class Form1
    Private Const WM_SETTEXT As Integer = &HC
    Private Const WM_GETTEXT As Integer = &HD
    Private Const WM_GETTEXTLENGTH As Integer = &HE

    <DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
    Private Shared Function FindWindowExW(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszClass As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszWindow As String) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="SendMessageW")> _
    Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Integer
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Find the parent window
        Dim hParent As IntPtr = FindWindowExW(IntPtr.Zero, hParent, Nothing, "Height Measurement")
        If hParent <> IntPtr.Zero Then

            'Find the 6th control in the parent window
            Dim hChild As IntPtr = FindWindowExW(hParent, hChild, Nothing, Nothing)
            For x As Integer = 0 To 4
                hChild = FindWindowExW(hChild, hChild, Nothing, Nothing)
            Next
            If hChild <> IntPtr.Zero Then

                'Find the Label in the child window and get its text
                Dim hLabel As IntPtr = FindWindowExW(hChild, IntPtr.Zero, Nothing, Nothing)
                For x As Integer = 0 To 13
                    hLabel = FindWindowExW(hChild, hLabel, Nothing, Nothing)
                Next
                If hLabel <> IntPtr.Zero Then
                    Dim len As Integer = SendMessageW(hLabel, WM_GETTEXTLENGTH, 0, Nothing)
                    Dim txt As String = Space(len + 1)
                    SendMessageW(hLabel, WM_GETTEXT, len + 1, txt)
                    TextBox1.Text = txt
                End If

                'Find the Textbox Edit window in the child window and set its text
                Dim hEdit As IntPtr = FindWindowExW(hChild, IntPtr.Zero, Nothing, Nothing)
                For x As Integer = 0 To 17
                    hEdit = FindWindowExW(hChild, hEdit, Nothing, Nothing)
                Next
                If hEdit <> IntPtr.Zero Then
                    SendMessageW(hEdit, WM_SETTEXT, 0, TextBox2.Text)
                End If

            Else
                MessageBox.Show("Child Window Not Found")
            End If

        Else
            MessageBox.Show("Main Window Not Found")
        End If
    End Sub
End Class


Hope that helps. B)/>

This post has been edited by IronRazer: 10 April 2014 - 06:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 10 April 2014 - 11:56 PM

Thanks you so much . I will try as your advice.

This post has been edited by little_prince41: 10 April 2014 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 19 May 2014 - 06:57 PM

your code is work, I have one more question for ask you how to know that which is another app show or hide ?
I need to separate status appear or hide.

Please, help me.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 20 May 2014 - 03:38 AM

Hi,
I am not sure what you mean. Do you mean you want to be able to tell if the other app is actually opened or maybe if it is Visible or not? Maybe you mean you want to be able to hide or show the other application?
I have things i have to do today so i will check in later when i get home to see what you say. B)
Was This Post Helpful? 0
  • +
  • -

#6 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 05 June 2014 - 08:23 PM

Posted Image

As my picture . I want to know status(check)of App or form that it's visible (1) or not (2) , how to check it?
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 06 June 2014 - 03:01 PM

Hi,
You can use the IsWindowVisible API function to find if a window is Showing or Hidden. First you will need to get the handle to the window though. You can use the FindWindow or FindWindowEx function to do that. Being you already have the FindWindowEx API function in your code from my prior example code i will show you an example using that function to find the window with the title of Message and will tell you if it is visible or not, or even if the window is not found. Of coarse that would mean the window is not opened at all.
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
    Private Shared Function FindWindowExW(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszClass As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszWindow As String) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="IsWindowVisible")> _
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Find the handle to the window with the title "Message"
        Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, Nothing, "Message")

        'Check if the window was found by seeing if the handle is other than IntPtr.Zero
        If hWnd <> IntPtr.Zero Then
            'Check if the window is visible
            If IsWindowVisible(hWnd) Then
                MessageBox.Show("Window is visible")
            Else
                MessageBox.Show("Window is not visible")
            End If
        Else
            MessageBox.Show("Window Not Found")
        End If
    End Sub
End Class


This post has been edited by IronRazer: 06 June 2014 - 03:03 PM

Was This Post Helpful? 3
  • +
  • -

#8 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 21 July 2014 - 03:04 AM

Thanks you IronRazer so much, you code is work, I need more function for check that button was click or not.I searched many web but i still not find solution yet.
Can you help me, please :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 21 July 2014 - 05:12 AM

What button are you talking about? If it is a button on another application that you want to check to see if it is clicked then that is going to be much more difficult thing to do. :unsure:

If any of my posts where helpful in getting you to this point then you can click the green (+) at the bottom of the posts. I don`t mind helping and taking time to make examples but, i appreciate being rewarded for it too. :)
Was This Post Helpful? 0
  • +
  • -

#10 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 73
  • View blog
  • Posts: 571
  • Joined: 05-December 13

Re: Control another app (Spy++)

Posted 21 July 2014 - 07:06 AM

When someone gives you as much help as you got above, its courteous to thank the user. Raising participation levels is the only form of a thank you we get.

As for the button you are referring to, it is not in your initial code, so please post the code for that button and kindly explain what it is you are trying to do.
Was This Post Helpful? 1
  • +
  • -

#11 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 22 July 2014 - 01:52 AM

View PostIronRazer, on 21 July 2014 - 05:12 AM, said:

What button are you talking about? If it is a button on another application that you want to check to see if it is clicked then that is going to be much more difficult thing to do. :unsure:/>


It is a button on another application . I need to know when it was press how to check it?
@I'm so sorry I forgot click the green (+) at the bottom of the posts
Was This Post Helpful? 0
  • +
  • -

#12 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 22 July 2014 - 02:58 AM

Hi,
Does the button cause anything to happen such as a messagebox or another window to pop up or maybe something on the window to change in some way like a label`s text to change or anything like that?

It is quite hard to tell when a button on a 3rd party application is pressed. You can`t receive a button click event like you would on your own application. The only ways i can think of is if one of the above happens when it is clicked or you would have to find the buttons handle on the 3rd party application and get the rectangle bounds of where it is on the screen and then monitor the mouse continuously to see if it is clicked within that rectangle area.

That can be done but, it will be quite a bit of work. B)
Was This Post Helpful? 1
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 22 July 2014 - 03:18 AM

I have to go to work for a while so i probably wont get back to you for a few hours at least. :)
Was This Post Helpful? 0
  • +
  • -

#14 little_prince41  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 10-August 07

Re: Control another app (Spy++)

Posted 22 July 2014 - 03:39 AM

I'm not sure , are you understand me or I'm not understand you I'm so sorry!.
I just want to know that a button press or not, I don't want to know what's happy when a button press.
@ Should I monitoring rectangle area of a button and how to get the rectangle bound ?

@ Thanks you so much for your time for help me. :bigsmile:

This post has been edited by little_prince41: 22 July 2014 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#15 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 371
  • View blog
  • Posts: 1,083
  • Joined: 01-February 13

Re: Control another app (Spy++)

Posted 22 July 2014 - 08:36 AM

Quote

I'm not sure , are you understand me or I'm not understand you I'm so sorry!.
I just want to know that a button press or not, I don't want to know what's happy when a button press.


No need to be sorry. :) If i understand you rite, you want to know when a button is clicked on a 3rd party window. Meaning a window that is not part of your program. Is that correct?

If so then what i am saying is there is no way to receive a Button click event in your program when a button in a 3rd party program is clicked.

The reason i ask if a window or messagebox pops up or something changes on the 3rd party window when the button on it is clicked is because it would be easier to check if a new window has been opened than it would be to keep monitoring the mouse to see if it was clicked in the same spot as the button on the 3rd party window.

If nothing changes on the 3rd party window and it does not open another window or messagebox and you need to monitor the mouse then you will need to run Spy++ and find the window tree to the Button you want to monitor and show me the image of the Spy++ tree like you did in your original post. Then i can put together an example of how to find it and monitor its location on the screen.

It may be easier to use the FindWindowEx API in some cases and EnumChildWindows API in other cases. You will also have to use the RECT structure, GetWindowRect API, and maybe another one or two API functions to do this.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2