I am trying to run an external application from my VB application (VS 2008). Although, I can click on the buttons on the primary window of this external executable, I cannot seem to collect the handle on "Save As" window that appears upon clicking a particular button on the primary window of this external executable.
Here is what I have done so far:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'This part is tried and tested up to a point.
Dim p As Process
Dim matches() As Process = Process.GetProcessesByName("test1")
If matches.Length = 0 Then
p = Process.Start("C:\Users\test\Desktop\demo1\test1\test1.exe")
Thread.Sleep(100)
ShowWindow(p.MainWindowHandle, 2)
Else
p = matches(0)
ShowWindow(p.MainWindowHandle, 2)
End If
Dim hWndMsgBox, hWndButton As Long
hWndMsgBox = FindWindow("#32770", "Test1 Application")
Debug.Print(Hex(hWndMsgBox))
Dim ChildHandles() As IntPtr = GetChildWindows(hWndMsgBox)
For i As Integer = 0 To ChildHandles.Length - 1
'Debug.Print(System.Convert.ToString(ChildHandles(i).ToInt32, 16))
Debug.Print(Hex(ChildHandles(i).ToInt32)) 'At this point I am able to return the hex values of all child handles to the primary window.
Next
If hWndMsgBox Then hWndButton = FindWindowEx(hWndMsgBox, 0&, "Button", "Start Job")
If hWndButton Then SendMessage(hWndButton, BM_CLICK, 0&, 0&)
Thread.Sleep(100)
If hWndMsgBox Then hWndButton = FindWindowEx(hWndMsgBox, 0&, "Button", "Save Results")
If hWndButton Then SendMessage(hWndButton, BM_CLICK, 0&, 0&)
hWndMsgBox = FindWindowEx1(hWndMsgBox, 0&, "#32770", "Save As")
Debug.Print(hWndMsgBox) 'This results in 0. And this is where I am stuck. Using Spy++, I found that the actual "Save As" dialog box is sub-classed.
hWndButton = FindWindowEx(hWndMsgBox, 0&, "Button", "Cancel")
If hWndButton Then SendMessage(hWndButton, BM_CLICK, 0&, 0&)
End Sub
I have tried to adapt as much as possible with the help of the following links: http://www.vbforums.com/showthread.php?567473-findwindowEX-Send-Message-in-Child-of-a-Child-of-an-Application
How to bring external application window on top?
How to set focus on other application based on process name VB
https://www.pinvoke.net/default.aspx/user32.EnumWindows
http://www.a1vbcode.com/snippet-5324.asp
http://vbnet.mvps.org/index.html?code/enums/enumwindows.htm
I must be overlooking something that is obvious. I can certainly do with any help offered.