Search code examples
automationui-automationshellexecuteex

UI Automation doesn't see hidden windows?


I have winforms application that is signed and has manifest with level="requireAdministrator" uiAccess="false".
I want to start another application with hidden window and to work with it using UI Automation API.

        Process procinst = new Process();
        procinst.StartInfo = new ProcessStartInfo()
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = true,
            ErrorDialog = true,
            Verb = "runas",
            FileName = appfilepath
        };           
        procinst.Start();

The new process with hidden window can be seen in Spy++ but not in UISpy and I cannot also find it using FindFirst method:

mainwnd = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "apptitle"));

When ProcessWindowStyle is Minimized everything goes fine, when it is Hidden - everything stops.
Is it expected UI Automation behavior? UI Automation cannot access hidden windows? Is there any way to hack it?


Solution

  • That's sad but that's true:

    There is not really such a thing as an “invisible” or “background” elements in UIA – when an element is gone, it will no longer be in the UIA tree. This is a good thing for assistive technologies (less pruning of the tree required), but can be troublesome for UI Automation frameworks. Historically we would tend to cache UI elements so we could get back to them again faster. When needed, one could call a mix of ShowWindow and accSelect with SELFLAG_TAKEFOCUS. This no longer works. If, say, a tool window is tabbed and not in the foreground; our cached element for it will because invalid, and if it’s requested we need to a) bring it into the foreground and b) find the element from scratch again.

    WPF in Visual Studio 2010 – Part 6 : Automated UI Testing