Application Crash issue with Windows UIAutomation Using C#

rohit patil 1 Reputation point
2021-09-09T05:32:42.947+00:00

Hello,

I have developed a Windows UI automation tool using C#. I am automating an external Windows application with this tool. It was working well untill one of the Windows update installed on my machine.

From my c# side code, I am getting below exception:

A first chance exception of type 'System.Windows.Automation.ElementNotAvailableException' occurred in UIAutomationClient.dll

This exception comes whenever I locate any control with mouse on external application.
Now, when I debug the external application itself, then I am getting below exception:

Exception thrown at 0x577960F6 (UIAutomationCore.dll) in XXX.exe: 0xC0000005: Access violation reading location 0xC218C48F.

I do not understand relationship between these two exceptions.
Could you please tell me the relation and why these errors come?

Details:
OS: Windows 10
Build:Version 20H2 (OS Build 19042.1165)
Visual Studio 2015

Thank you in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,945 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 65,371 Reputation points
    2021-10-04T09:44:07.907+00:00

    I made a minimal sample with ElementFromPoint to get IUIAutomationElement under the mouse,
    you can check if it crashes too (cannot test on my OS (Windows 10 1909)=>

    137364-uia-elementfrompoint.jpg

    Reference + using :

    // Add reference to : C:\Windows\System32\UIAutomationCore.dll (UIAutomationClient : Embed Interop Types = False)  
    // Add : using UIAutomationClient;  
    

    Test :

    public partial class Form1 : Form  
    {  
        private System.Windows.Forms.RichTextBox richTextBox1;  
        private System.Threading.Thread workerThread = null;  
        private readonly System.Threading.AutoResetEvent isCanceled = new System.Threading.AutoResetEvent(false);  
        IUIAutomation uiA = null;  
      
        public Form1()  
        {  
            //InitializeComponent();  
            this.Text = "Form1";  
            this.ClientSize = new System.Drawing.Size(540, 140);  
            this.Load += new System.EventHandler(this.Form1_Load);  
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);             
        }  
      
        private void Form1_Load(object sender, EventArgs e)  
        {  
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();  
            this.richTextBox1.Location = new System.Drawing.Point(20, 20);  
            this.richTextBox1.Name = "richTextBox1";  
            this.richTextBox1.Size = new System.Drawing.Size(500, 100);  
            this.richTextBox1.ReadOnly = true;  
            this.richTextBox1.Text = "";  
            Controls.Add(this.richTextBox1);  
      
            uiA = new CUIAutomation();  
      
            workerThread = new System.Threading.Thread(BackgroundTask);  
            workerThread.IsBackground = true;  
            workerThread.Name = "Background Task";  
            workerThread.Start();  
      
            CenterToScreen();  
        }  
      
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            workerThread.Abort();  
        }  
      
        private void BackgroundTask()  
        {  
            for (; !isCanceled.WaitOne(0);)  
            {  
                System.Drawing.Point point = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);  
                tagPOINT tp = new tagPOINT();  
                tp.x = (int)point.X;  
                tp.y = (int)point.Y;  
      
                string sCurrentName = null;  
                string sCurrentType = null;  
                string sCurrentValue = null;  
                string sCurrentText= null;  
                // UIA_E_ELEMENTNOTAVAILABLE {"Un événement n’a pu invoquer aucun des abonnés. (Exception de HRESULT : 0x80040201)"} System.Runtime.InteropServices.COMException  
                try  
                {  
                    IUIAutomationElement element = uiA.ElementFromPoint(tp);  
                    if (element != null)  
                    {  
                        sCurrentName = element.CurrentName;  
                        sCurrentType = element.CurrentLocalizedControlType;  
                        Object pPatternValue = element.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId);  
                        if (pPatternValue != null)  
                        {  
                            IUIAutomationValuePattern pValuePattern = (IUIAutomationValuePattern)pPatternValue;  
                            sCurrentValue = pValuePattern.CurrentValue;  
                        }  
                    }  
                    sCurrentText = (sCurrentName != null ? sCurrentName : "");  
                    sCurrentText += (sCurrentValue != null ? Environment.NewLine + sCurrentValue : "");  
                    sCurrentText += (sCurrentType != null ? Environment.NewLine + sCurrentType : "");  
                    if (element.CurrentAutomationId != "richTextBox1")  
                        SetText(richTextBox1, sCurrentText);  
                    else  
                        SetText(richTextBox1, "RichEdit Control" + Environment.NewLine + "richTextBox1");  
                }  
                catch (System.Exception ex)  
                {  
                    string sErrorMsg = ex.ToString();  
                }  
            }  
        }  
      
        delegate void SetTextCallback(Control ctrl, string text);  
        private void SetText(Control ctrl, string sText)  
        {  
            if (ctrl.InvokeRequired)  
            {  
                SetTextCallback c = new SetTextCallback(SetText);  
                this.Invoke(c, new object[] { ctrl, sText });  
            }  
            else  
            {                
                ctrl.Text = sText;  
            }  
        }       
    }  
    
    No comments

  2. Thomas Grobicki 1 Reputation point
    2021-12-10T13:07:38.767+00:00

    I know that this question is about a programming situation, but I'm experiencing the same 0xC0000005: Access violation in UIAutomationCore.dll when running Microsoft Outlook 2013. When the situation happens, you see all Outlook windows with black frames around them and the program becomes unresponsive. This occurred after the most recent patch updates to Windows 10 Pro 21H2 build 19044.1387. The file date on the DLL is 10/6/2021 and the version of the DLL is 7.2.19041.1266 . The reason I'm posting in this discussion is that this is the most recent complaint about this problem. There are a number of previous post, some even about Outlook, that describe the same situation. I have a number of programs on my system that successfully use UIAutomationCore.dll, so it could be an Outlook problem, but it only happens in this DLL and only after I updated Windows. I've tried dropping back to the previous release of these DLL's (one is located in system32 and the other in sysWOW64), but that caused other issues.

    No comments