rohitpatil-7347 avatar image
0 Votes"
rohitpatil-7347 asked Castorix31 answered

Application Crash issue with Windows UIAutomation Using C#


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.

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please show the code that caused the error?
What version of your previous operating system?

0 Votes 0 ·

Sorry, I can not share the code.
but On inserting the older version of DLL in the installation directory, the Automation tool does not crash the appliaction.
The version copied to the installation directory is: 7.2.17763.1728

could you please help me out why this happned?

0 Votes 0 ·

I don't know how to reproduce this problem.
Since this problem is caused by installing a Windows update, can we solve the problem by uninstalling this update?
How to uninstall updates and patches in Windows 10
Uninstall Windows 10 Updates

0 Votes 0 ·

Does MS Inspect tool work ?
If yes, the problem is in your code...

0 Votes 0 ·

Thank you for the replay,
I didn't use MS Inspect yet.
could you please tell me how I have to test with inspect to confirm my problem is with my code or system.

0 Votes 0 ·
Castorix31 avatar image Castorix31 rohitpatil-7347 ·

Just to check if it crashes when it displays the hierarchy of controls under the mouse

0 Votes 0 ·
Show more comments

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered

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;

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.