System.ComponentModel.Win32Exception occurs with Parameter is incorrect message while accessing control through automation

1.2k Views Asked by At

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.

0

There are 0 best solutions below