Search code examples
c#selectionui-automationuiaccessibilitywindows-11

UIAutomation throws AccessViolationException on Windows 11


The issue:

We have an application written in C# that uses UIAutomation to get the current text (either selected or the word behind the carret) in other applications (Word, OpenOffice, Notepad, etc.).

All is working great on Windows 10, even up to 21H2, last update check done today. But we had several clients informing us that the application is closing abruptly on Windows 11.

After some debugging I've seen some System.AccessViolationException thrown when trying to use the TextPatternRange.GetText() method:

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

What we've tried so far:

Reproductible example

In order to be able to isolate the issue (and check it was not something else in our app that was causing the exception) I quickly made the following test (based on : How to get selected text of currently focused window? validated answer)

private void btnRefresh_Click(object sender, RoutedEventArgs e)
    {
        var p = Process.GetProcessesByName("notepad").FirstOrDefault();
        var root = AutomationElement.FromHandle(p.MainWindowHandle);

        var documentControl = new
                PropertyCondition(AutomationElement.ControlTypeProperty,
                                  ControlType.Document);

        var textPatternAvailable = new PropertyCondition(AutomationElement.IsTextPatternAvailableProperty, true);

        var findControl = new AndCondition(documentControl, textPatternAvailable);

        var targetDocument = root.FindFirst(TreeScope.Descendants, findControl);
        var textPattern = targetDocument.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

        string text = "";
        foreach (var selection in textPattern.GetSelection())
        {
            text += selection.GetText(255);
            Console.WriteLine($"Selection: \"{selection.GetText(255)}\"");
        }

        lblFocusedProcess.Content = p.ProcessName;
        lblSelectedText.Content = text;
    }

When pressing a button, this method is called and the results displayed in labels. The method uses UIAutomation to get the notepad process and extract the selected text.

This works well in Windows 10 with latest update, crashes immediately on Windows 11 with the AccessViolationException. On Windows 10 it works even without the uiaccess=true setting in the manifest.

Questions/Next steps

Do anyone know/has a clue about what can cause this? Is Windows 11 way more regarding towards UIAutomation?

On my side I'll probably open an issue by Microsoft. And one track we might follow is getting an EV and sign the app itself and the installer as it'll also enhance the installation process, removing the big red warnings. But as this is an app distributed for free we had not done it as it was working without it.

I'll also continue testing with the reproductible code and update this question should anything new appear.


Solution

  • I posted the same question on MSDN forums and got this answer: https://learn.microsoft.com/en-us/answers/questions/915789/uiautomation-throws-accessviolationexception-on-wi.html

    Using IUIautomation instead of System.Windows.Automation works on Windows 11.

    So I'm marking this as solved but if anyone has another idea or knows what happens you're welcome to comment!