1

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

https://social.msdn.microsoft.com/Forums/vstudio/en-US/dd799cce-4525-4a2e-9c40-1d831d3b4e4f/unable-to-write-the-filename-and-click-on-save-button-simultaneously8230vba-code?forum=vbgeneral

Send text to "save as" dialog

How to bring external application window on top?

How to set focus on other application based on process name VB

https://www.dreamincode.net/forums/topic/312740-how-to-click-a-button-in-an-external-application-from-within-vbnet/

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.

Fruitjam
  • 21
  • 4
  • Have you used [Spy++](https://learn.microsoft.com/en-gb/visualstudio/debugger/introducing-spy-increment) to investigate the handle? – Andrew Morton Jun 07 '18 at 13:01
  • I did. The handle returned, though pointed correctly to the primary window handle, did not help. Following are the hex values of child windows that are being returned: 100386 840F78 1402B8 90578 1103F0 1003A2 780FD8 20103A 100570 D0650 D0652 4B0FBC 4E0F94 1A03C8 16044A The first value matches with that as detected by Spy++ for the primary window. The value of the "Save As" dialog box, however, does not match with any of the listed above. – Fruitjam Jun 07 '18 at 13:38
  • Trying to get the external (3rd party) application first running, and then trying to obtain the handle of the "Save As" dialog box yields results. When I start the external software from my VB application and then try to obtain the handle of the "Save As" dialog box, my VB application fails. It does, however, return the handle of the primary window. – Fruitjam Jun 08 '18 at 06:11
  • 2
    Finally, I found a way to approach the problem. By running another custom-built VB form to collect active handle of the "Save As" dialog box, I realized it was a matter of separating the processes. The direction, therefore, that I took was to initiate separate threads - one to fire up the external application, and the other to detect the handle of the "Save As" dialog box. – Fruitjam Jun 08 '18 at 13:22
  • That sounds like something that you could usefully make into an answer for this question. (I'm not sure if you have to wait a while before marking an answer of your own as accepted.) – Andrew Morton Jun 08 '18 at 13:26
  • That is most kind of you Andrew Morton. – Fruitjam Jun 08 '18 at 16:09

0 Answers0