Control other application

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
I'm trying to build an application that will automaticly install something for you, though I can't seem to figure out how I can control an other window. I googled for a couple of days with the keyword window handle but I don't really know what I should be asking for. What I want to do is simple, VB.net will start an application and I wan't to be able to push buttons from my VB program in that application (like clicking next and checking a checkbox).
 
Most setup programs let you specify "Silent" installs which you can predefine all the options.

In case yours doesnt you will need to look up how to get a window handle and send that window a message, all the buttons and checkboxed are also classed as windows and have handles too. so you will need to first get the handle of the applications main form, then you can enumerate through all the child windows in that window, then all you have to do is send that window a click message.

Look up things like user32 API and sendmessage()
 
Thx for the quick reply, I think I'm finally getting somewhere.
I created a little test form called form1 with a button called button1, when the button is pressed a messagebox appears, I compiled it and saved it on my C drive.
The I created an other form called testing with a button. When the button is pressed it should start my c:\test.exe and press the button so that the messagebox should appear. This is the code I wrote:
VB.NET:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Public Const WM_CHAR = &H102

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Process.Start("c:\test.exe")
        Dim Hwnd As Long = FindWindow("form1", vbNullString)
        MsgBox(CStr(Hwnd))
        Dim HwndEx As Long = FindWindowEx(Hwnd, 0, "Button1", vbNullString)
        SendMessage(HwndEx, WM_CHAR, Keys.Enter, 0)
    End Sub
Problem now is that I get an overflow exception when I press the button and it highlights the line where I use the FindWindowsEx function.
<edit> Silly me I found the error and fixed it, It still doesn't work though.
 
if your using vb.net you have to change long to integer, and sometimes to intptr if its a handle...

VB.NET:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Intptr
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Intptr, ByVal hWnd2 As Intptr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Intptr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Intptr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Const WM_CHAR = &H102
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("c:\test.exe")
Dim Hwnd As Intptr = FindWindow("form1", vbNullString)
MsgBox(CStr(Hwnd))
Dim HwndEx As Intptr = FindWindowEx(Hwnd, 0, "Button1", vbNullString)
SendMessage(Hwnd, WM_CHAR, Keys.Enter, 0)
End Sub

try that, i havnt had time to test it but i think thats where its going wrong, API calls are verry perticular on what types you use.

Im going out no so i wont be able to reply for a while, so good luck :)
 
Last edited by a moderator:
Still doesn't work, I'm thinking that I might be using the wrong API to press my button but I can't find the one I need, can someone help me how to locate the handle to my button. Any Help is grately apreciated since I need this for my Master work in school.
 
Download a program called WInSpector and use it to look at the various window commands that float round the system. If your app is trying to click a button in another app, then sending it an enter key won't work. There is a specific window message for clicking, and I think it may be called WM_COMMAND, but it has been so long since I was writing code that manipulated other windows, that I've kinda lost it. I will take a look in what is left of my source code lib from VB6 days - i lost most of it in a hard drive failure, but i may still be able to find a couple of modules I wrote for finding windows, sizing and clicking them etc.
 
VB.NET:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Intptr
 
 
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Intptr, ByVal hWnd2 As Intptr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Intptr
 
 
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Intptr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
 
Public Const WM_CHAR = &H102
 
 
Dim HwndEx As Intptr = FindWindowEx(Hwnd, 0, "Button1", vbNullString)

Intptr's or integers, it doesn't matter you can use either, they are interchangable. But long's and integers are a different story. The code above shouldn't event compile since the second argument in FindWindowEx is an intptr and you are passing an integer, to my mind the second argument should be 'intptr.zero'
 
It works now, thx allot. :)

I downloaded a small program wich returns the classname of the button I'm on and I saw that I had to input "Button" for my classname and "Button1" as my windowname.
 
Ahhh, interesting. I messed around with it to. I had a bit of trouble til you said bout the classname thing. All good now :)
 

Latest posts

Back
Top