0

I am trying to access a wpf control using AutomationElement but sometimes it throws and exception. Below is the code:

if (automationElement.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) 
{
    ValuePattern valuePattern = pattern as ValuePattern; 
    valuePattern.SetValue(valueToBeSet); 
}

I am doing till here, this triggers some methods calls in MS.Win32.UnsafeNativeMethods which then gives the exception.

Stack trace from event log:

Exception Messages: 
    Win32Exception      : The parameter is incorrect

Type         : System.ComponentModel.Win32Exception, System, Version=4.0.0.0, Culture=neutral
Message      : The parameter is incorrect
Source       : WindowsBase
NativeErrorCode: 87
ErrorCode    : -2147467259
TargetSite   : Int32 GetWindowText(System.Runtime.InteropServices.HandleRef, System.Text.StringBuilder, Int32)
Stack Trace  :    at MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount)
        at System.Windows.Automation.Peers.WindowAutomationPeer.GetNameCore()
        at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
        at System.Windows.ContextLayoutManager.fireAutomationEvents()
        at System.Windows.ContextLayoutManager.UpdateLayout()
        at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
        at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
        at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
        at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
        at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
        at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

The WPF control is standard WPF control(No customization). I am not able to cause of this error. I believe it is the window handle which is incorrect(param for GetWindowText) but have no idea how to check this? In case it is something else please do tell.

Kryptonian
  • 860
  • 3
  • 10
  • 26
  • 1
    WPF controls don't have handles. Just the Window does. Use the Automation Patterns ([TextPattern](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.textpattern)) to access the Text content. If that pattern is supported, of course. [TryGetCurrentPattern](https://learn.microsoft.com/en-us/dotnet/api/system.windows.automation.automationelement.trygetcurrentpattern) helps to determine if it does. – Jimi Aug 31 '19 at 19:17
  • Yes, I am accessing the Automation pattern, for getting the automation pattern from the control windows is calling these methods internally over which I have no control and which throws the exception. The stack trace is from event log. The control has text pattern available as I have checked it and also I am able to enter text generally(The issue is sporadic). – Kryptonian Aug 31 '19 at 19:29
  • 1
    So, do you have an exception while trying to get a Window handle? Using a Window handle that you got somehow? It's not clear. Post the code. – Jimi Aug 31 '19 at 19:34

0 Answers0