1

I have a simple function which works about like this:

let possibleParents:Seq<Process> = GetProcessesByExecutableFileName(exeName)
let el = match possibleParents with
            | x when x.Where(fun y -> y.MainWindowHandle > IntPtr.Zero).Count() = 1 -> AutomationElement.FromHandle <| Seq.head possibleParents // Only works if no tooltips or menus are open
            | x when x.Where(fun y -> y.MainWindowHandle > IntPtr.Zero).Count() < 1 -> AutomationElement.FromHandle <| Win32.FindWindowByProcess exeName //ALWAYS works
            | x when x.Count() > 1 -> AutomationElement.RootElement // start search from Desktop
            | _ -> null

So, when the getting the Process object from .NET, it returns IntPtr.Zero if there are any menus\contextmenus, tooltips or comboboxes open. As soon as you move your mouse away from a tooltip, it returns a real IntPtr value.

Why is this? I can understand mdi children & other window hierarchy causing this, but not simply mousing over a tooltip. What does user32.dll imports do to find the Handle that the Process class does differently?

Answers in F#, C# or VB.NET welcome.

turkinator
  • 905
  • 9
  • 25
  • 2
    [This post](http://stackoverflow.com/questions/48288/unexpected-behaviour-of-process-mainwindowhandle) indicates that `.MainWindowHandle` just returns the top most window, maybe the tooltip is causing it to return zero? And also that post is 8 years old so it may be very outdated by now.. – Quantic Sep 07 '16 at 21:55

0 Answers0