WinForms / WPF - MS.Win32.UnsafeNativeMethods.GetWindowText

Lars Larsson 31 Reputation points
2021-08-24T13:02:00.093+00:00

Seeing a looping of throws System.ComponentModel.Win32Exception.
Can see it using ProcesExplorer or enable native & disable "just my code" - otherwise it goes by "undetected"

How does one troubleshoot it - quite complex application with WinForms & WPF (ElementHost), have not touched any AutomationPeer occurs in different parts of code usually during visibility toggle (i.e. false) or close. But how to root cause it. Once it triggers it keeps retriggering i.e. App.Run seeing thrown C# exception increase all the time.

System.ComponentModel.Win32Exception
  HResult=0x80004005
  Message=Invalid window handle
  Source=WindowsBase
  StackTrace:
   at MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount)

  This exception was originally thrown at this call stack:
    MS.Win32.UnsafeNativeMethods.GetWindowText(System.Runtime.InteropServices.HandleRef, System.Text.StringBuilder, int)

Target framework 4.8 but some might be older, plugin based and got history back from .NET 1.0 but don't think its related.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,841 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,683 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Lars Larsson 31 Reputation points
    2021-08-24T14:24:21.02+00:00

    I can get it in the debugger and reproduce it but I cannot retrive any valuable information / make sense of it. My guess is its related to ElementHost or at least google returns those that use ElementHost or UI libraries where they overriden AutomationPeer related.

    But looking for known issues / or any good way to actually get out more information or if this is "normal" operation.

    Sample call stack

    ot Flagged    >    22080    1    Worker Thread    Main UI 1    WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetWindowText
                                  WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetWindowText(System.Runtime.InteropServices.HandleRef hWnd, System.Text.StringBuilder lpString, int nMaxCount)
                                  PresentationCore.dll!System.Windows.Automation.Peers.GenericRootAutomationPeer.GetNameCore()
                                  PresentationCore.dll!System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
                                  PresentationCore.dll!System.Windows.ContextLayoutManager.fireAutomationEvents()
                                  PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout()
                                  PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayoutCallback(object arg)
                                  PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
                                  PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget)
                                  PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object resizedCompositionTarget)
                                  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)
                                  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler)
                                  WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl()
                                  WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj)
                                  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
                                  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
                                  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
                                  WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
                                  WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke()
                                  WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue()
                                  WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)
                                  WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)
                                  WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o)
                                  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)
                                  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler)
                                  WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)
                                  WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam)
                                  [Native to Managed Transition]
                                  user32.dll!00007ffa3ea0e858()
                                  user32.dll!00007ffa3ea0e299()
                                  System.Windows.Forms.ni.dll!00007ffa060eb6d9()
                                  [Managed to Native Transition]
                                  System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData)
                                  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context)
                                  System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context)
                                  MyProgream.Program.Main() Line 196
                                  [Native to Managed Transition]
                                  mscoreei.dll!00007ffa19ef8c01()
                                  mscoree.dll!00007ffa19fdac42()
                                  kernel32.dll!00007ffa3f2b7034()
                                  ntdll.dll!00007ffa3f4a2651()
     
    
    1 person found this answer helpful.

  2. Michael Taylor 48,976 Reputation points
    2021-08-24T14:17:22.693+00:00

    In the debugger go to the exception settings. Find the Win32Exception and tell the debugger to break when it is thrown. Then run your app. As soon as anybody throws the exception the debugger will break in.

    The actual problem is that somebody is trying to get the window text from a bad window handle. Nevertheless it is very common to have thrown exceptions. You generally won't see them because somebody handles them. By setting the debugger to break on throw you'll see them all, which could be too much.

    The other scenario is that it could be thrown on a secondary thread and the thread is terminated. Normally this will crash your app as defined here. But you mentioned 1.0 so I'll say that in that release unhandled exceptions are silently handled as discussed in the link already given. However you wouldn't be hosting a 1.x assembly in a .NET 4 app so this shouldn't be an issue.

    Outside the debugger option mentioned earlier, if the exception is being handled by somebody else there isn't a good way to track it as you won't get notified, because it is already handled.