Can .Net (VB) run an external file AND manipulate it?

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
I know about Automation, but this isn't that sort of case.

I know you can run an external exe file. But can you manipulate it someway?

Here is the situation, and it's not complicated:

A file that opens up, the form has a "From" and a "To" text box, then a button "Start".

We want to create a "controller" app with VB.Net so we can set it to run automatically at certain times.

The form always opens up with blank text boxes so you have to fill it out every time.

If it were a simple file/path/directory backup I'd just write it, but this file shuts down databases, copies them, sets some switch in the copied databases to "storage" with the time and date, then re-activates all the origional databases when finished.

Since I know VB can cause the application to execute, I'm thinking there might be a way to get the focus to one text box, vb sends it a path, jumps to the next textbox and gives it another path, then clicks the "run" button.

Is there a way to do this, maybe having VB simulating mouse clicks at certain coordinates and then "type" out a string?

This would make creating an "Automated Shell" program for simple apps a really sweet deal!
 
If im understanding you correctly you can do something like this. Start a process using the Process class, and get the handle to the executable from the process object and then use EnumChildWindows and SetWindowText APIs.
 
To simplify things:

1. I want to write specific text into one textbox.

2. I want to write different text into the second textbox.

3. click the "Start" button.

If this works, I can see it opening up a whole lot of possibilities in the office.

Is this how you run it using the process class:

Visual Basic:
Process.Start("Filename")

I'm just not sure how to get the handle from that, but I'm sure I can figure out EnumChildWindows and SetWindowText API's.

Thanks for the pointer, Its a place to start snooping :)
 
Create a new Process object instead of just using the shared Start() method:
Visual Basic:
Dim p As New Process
Then you can use all kinds of interesting methods and properties of the process.
 
ok, I looked around and found out how to declare the functions in question, but I'm not sure exactly how I can access any of the textboxes or buttons on this application.

"TestApp" is an application with two Textboxes and a Button.



Visual Basic:
'proc is a process to get the handle for the application in question.
    Dim proc As Process
    'hHandle is the variable for the application
    Dim hHandle As System.IntPtr

    'Declaration of Callback, a required function 
    'for EnumChildWindows.
    Public Delegate Function CallBack( _
        ByVal hwnd As Integer, _
        ByVal lParam As IntPtr) As Boolean

    Declare Function SetWindowText Lib "user32" _
        Alias "SetWindowTextA" (ByVal hwnd As Long, _
        ByVal lpString As String) As Long

    Public Declare Function EnumChildWindows Lib "user32" ( _
        ByVal hwnd As Integer, _
        ByVal lpEnumFunc As CallBack, _
        ByVal lParam As Integer) As Integer

    Public Shared Function ReceiveWindowHandles(ByVal hwnd As Integer, ByVal lParam As IntPtr) As Boolean

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'In the Form1_Load I have to run through the array of Processes
        'because I don't know how to directly set the name to "TestApp"
        'Once I get to "TestApp", I set hHandle to "TestApp"'s handle.
        'I've successfully closed "TestApp" and gotten info on it, so 
        'I was successful getting to it.
        proc.GetProcesses(Environment.MachineName)
        For Each proc In proc.GetProcesses
            If proc.ProcessName = "TestApp" Then
                hHandle = proc.Handle
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EnumChildWindows(hHandle.ToInt32, AddressOf Me.ReceiveWindowHandles, 0)
    End Sub

From what I've read ReceiveWindowHandles is supposed to get the handles? How do I access them?
 
It passes them to the hWnd argument, but I'm not sure how to access them.

I have no clue what I'd use to get access to the first textbox, the second text box or the button...

I'm still looking into it, but there isn't much about this on these forums... hmm, they're old API's so maybe hitting up the vb6 forums might be a good idea.
 
Might be Jury Rigged, but it works :)

Ok, it works.

This is how I did it:

Visual Basic:
    Private Declare Function BringWindowToTop Lib "user32.dll" (ByVal hwnd As System.IntPtr) As Integer

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Process name is usually the executable name without the ".exe"
        Dim proc() As Process = Process.GetProcessesByName("TestApp")
        Dim x As Integer
       
        'Brings "TestApp" to focus
        BringWindowToTop(proc(x).MainWindowHandle)
        'Focus started in the first textbox, so I just send the text over
        SendKeys.Send("Text1")
        'Tab to the next textbox
        SendKeys.Send(vbTab)
        'Send text to the next textbox
        SendKeys.Send("Text2")
        'Tab to the button
        SendKeys.Send(vbTab)
        'A space activates a button with focus
        SendKeys.Send(" ")
    End Sub

This could be applied to any program which has keyboard functionality, which is nearly all of them.

It just requires you to to do a run through once to check the tab order and set your tabs accordingly.

I'm sure that there is a way to do this more 'slyly', in a more programer stylish way, but for non dynamic forms, this will get the job done :)
 
Back
Top