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.