Skip to content

System.ComponentModel.Win32Exception at MS.Win32.UnsafeNativeMethods.GetWindowText #7344

Closed

Description

  • .NET Version: 6.0.11
  • Windows version: 10.0.22000.0
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

I would have posted this as a comment on #4181 but that issue is locked.

We are getting a crash in an almost identical call stack to that issue:

Win32Exception: The parameter is incorrect. in MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount)
MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount):26
System.Windows.Automation.Peers.WindowAutomationPeer.GetNameCore():92
System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree():118
System.Windows.ContextLayoutManager.fireAutomationEvents():83
System.Windows.ContextLayoutManager.UpdateLayout():865
System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg):16
System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks():73
System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget):92
System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget):68
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler):58
System.Windows.Threading.DispatcherOperation.InvokeImpl():167
MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj):33
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state):135
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw():17
MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state):88
System.Windows.Threading.DispatcherOperation.Invoke():150
System.Windows.Threading.Dispatcher.ProcessQueue():313
System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled):217
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled):35
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o):96
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler):58
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs):242
MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam):63
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg):-1
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame):60
System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame):94
System.Windows.Threading.Dispatcher.Run():5
System.Windows.Application.RunDispatcher(Object ignore):31
System.Windows.Application.RunInternal(Window window):223
System.Windows.Application.Run(Window window):6
System.Windows.Application.Run():7
OurApp.Main(String[] astrArgs):448

The crash from the previous major version of the application, which used WPF on .NET Framework 4.8 is:

Win32Exception: The parameter is incorrect in MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount)
MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount):27
System.Windows.Automation.Peers.WindowAutomationPeer.GetNameCore():76
System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree():121
System.Windows.ContextLayoutManager.fireAutomationEvents():83
System.Windows.ContextLayoutManager.UpdateLayout():850
System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg):16
System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks():69
System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget):98
System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget):53
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs):119
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler):2
System.Windows.Threading.DispatcherOperation.InvokeImpl():122
MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj):34
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx):121
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx):9
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state):52
MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state):52
System.Windows.Threading.DispatcherOperation.Invoke():32
System.Windows.Threading.Dispatcher.ProcessQueue():246
System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled):144
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled):48
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o):54
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs):119
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler):2
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs):187
MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam):304
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg):-1
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame):64
System.Windows.Application.RunDispatcher(Object ignore):36
System.Windows.Application.RunInternal(Window window):238
OurApp.Main(String[] astrArgs):427

I've read the comments on #4181, but it's not clear to me why WindowAutomationPeer.GetNameCore "cannot ignore [an error from GetWindowText] without also ignoring genuine errors", or even just use Window.Title as the name instead of P/Invoking to native code to get the window title.

As per the following code, setting Window.Title syncs the HWND by calling SetWindowText, so it seems like it should be safe to read Window.Title in order to provide an automation name.

// called as a result of title property changing to propagate it to the hwnd
internal virtual void UpdateTitle(string title)
{
// Adding check for IsCompositionTargetInvalid
if ( IsSourceWindowNull == false && IsCompositionTargetInvalid == false)
{
UnsafeNativeMethods.SetWindowText(new HandleRef(this, CriticalHandle), title);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions