1

I want to launch an external application and get the handle of its Connect button. I get the top level "000A0B32" and I can get this 100% correct. Then I'm trying to get the child element "000F0C58" and get the Connect button under it. But getting "000F0C58" fails frequently. Instead of "000F0C58", you can get "000D0B94" of the same class at the top level.

Spy++

I'm expliciting the parent handle In FindWindowEx. I don't understand why I am getting irrelevant handles. Both "000F0C58" and "000D0B94" have no Caption and cannot add conditions for FindWindowEx.

I'm looking into how to get the matching stuff (like the process id) from all handles, but that's just an alternative. I believe that using FindWindowEx correctly will guarantee results, and I'd like to know what I'm doing wrong.

As you may have noticed, I am not good at English. Simple English or code sample will help me.

Process.Start(@"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe")
{
    Process[] vpnui = this.GetProcessesByName("vpnui");
    if (vpnui.Length != 0 && vpnui[0] != null)
    {
        mainChildHwnd = FindWindowEx(vpnui[0].MainWindowHandle, IntPtr.Zero, "#32770", null);
        if (mainChildHwnd != null && mainChildHwnd != IntPtr.Zero)
        {
            IntPtr connectButtonHwnd = FindWindowEx(mainChildHwnd, IntPtr.Zero, "Button", "Connect");
            if (connectButtonHwnd != null && connectButtonHwnd != IntPtr.Zero)
            {
                SendMessage(connectButtonHwnd, WM_LBUTTONDOWN, MK_LBUTTON, 0x000A000A);
                SendMessage(connectButtonHwnd, WM_LBUTTONUP, 0x00000000, 0x000A000A);
            }
        }
    }
}
okaken
  • 13
  • 4
  • For future reference, please take the time and make the effort to make your question as readable as possible. leaving a big wad of pointless leading whitespace in your code, necessitating horizontal scrolling, does not qualify. – jmcilhinney Apr 19 '23 at 07:28
  • Thank you! I have modified the question as per your indication. – okaken Apr 19 '23 at 07:31
  • While using the Win32 API does work doing this sort of stuff launching apps and clicking buttons is going out of fashion (security, different CPU architectures, Windows discontinued support). These days its definitely better to ask **vpnui** for a command line arg you can use to Connect. That will always work and surely they must have customers who also want this feature. I'm pretty sure helping you go down this oldskool direction is the wrong way to go. – Jeremy Thompson Apr 19 '23 at 07:35
  • On the occasions that it fails, what happens if you call `FinbdWindowEx` again and specify the previous child handle as the startying point for the search? – jmcilhinney Apr 19 '23 at 07:39
  • your advice is valid. But vpnui is not made by me and is difficult to implement. I have created this application for my own use, but I want to press a button in an external application. – okaken Apr 19 '23 at 07:41
  • > Jmcilhinney, Which handle is your previous child handle? Sometimes I can get "000F0C58" correctly in mainChildHwnd. But sometimes I get "000D0B94". I understand that specifying the child handle as the second argument is when I want to get another button handle in the same row as the Connect button. – okaken Apr 19 '23 at 07:46

0 Answers0