Application Crash issue with Windows UIAutomation Using C#

rohit patil 1 Reputation point


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?

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

Thank you in advance.

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.
10,640 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 83,106 Reputation points

    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)=>


    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()  
            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 = "";  
            uiA = new CUIAutomation();  
            workerThread = new System.Threading.Thread(BackgroundTask);  
            workerThread.IsBackground = true;  
            workerThread.Name = "Background Task";  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        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  
                    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);  
                        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 });  
                ctrl.Text = sText;  
    0 comments No comments

  2. Thomas Grobicki 1 Reputation point

    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.

    0 comments No comments