I have been seeing a pair of exceptions when debugging (VS 2015) which occur when clicking in a Textbox. They do not always occur but they always seem to occur under the same circumstances (I can't find a pattern which would explain it). They also only seem to occur once -- if I proceed on, ignoring them, I do not see them happen again. They also don't seem to be related to a specific Textbox. My code is a hybrid of existing code and add-on code and the same problem occurs in the code I haven't touched.
I tried creating a very simple project with just a Textbox in a window but I haven't seen the problem there (but again, it doesn't occur in all cases, anyway).
I cannot catch them in a try-catch -- the debugger detects them when they are thrown but it would appear that Microsoft is catching them internally and not throwing out of their code. If I try running outside the debugger my try-catch catches nothing and the code works with no apparent problems.
The first is a ArgumentNullException thrown by mscorlib.dll. If I continue after that is thrown, a PlatformNotSupportedException is thrown by PresentationCore.dll. If I continue after that, my code seems to run fine.
I will put details below but I am wondering, since they appear to be handled internally in Microsoft's code (and seem unrelated to my code) should I just presume it was something I was never intended to see? If that isn't the case can anyone suggest what might be happening here or how I might debug it?
My platform is Windows 7, I am building with "Any CPU" and the current target framework is .Net 4.5.2 (but I tried it with frameworks up to 4.6.1 with no apparent change).
The initial exception:
System.ArgumentNullException occurred
HResult=-2147467261
Message=Value cannot be null.
Parameter name: type
ParamName=type
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.GetActivationFactory(Type type)
InnerException:
A portion of the call stack when this is thrown:
mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.GetActivationFactory(System.Type type) Line 1279 C#
PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetWinRtActivationFactory(bool forceInitialization) Unknown
PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.InputPane() Unknown
[Native to Managed Transition]
[Managed to Native Transition]
PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetForWindow(System.Windows.Interop.HwndSource source = {System.Windows.Interop.HwndSource}) Unknown
PresentationCore.dll!MS.Internal.Interop.TipTsfHelper.Show(System.Windows.DependencyObject focusedObject) Unknown
PresentationCore.dll!System.Windows.UIElement.Focus() Unknown
PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor This = {System.Windows.Documents.TextEditor}) Unknown
PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationFramework.dll!System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(System.Windows.Input.MouseButtonEventArgs e) Unknown
PresentationCore.dll!System.Windows.UIElement.OnMouseDownThunk(object sender = {System.Windows.Controls.TextBox}, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown
PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = false) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown
PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown
PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input) Unknown
PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) Unknown
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) Unknown
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = {System.IntPtr}, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONDOWN, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler = null) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}) Unknown
[Native to Managed Transition]
The mscorlib code throwing the exception (due to a null type parameter) is this:
[SecurityCritical]
public static IActivationFactory GetActivationFactory(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
if (type.IsWindowsRuntimeObject && type.IsImport)
{
return (IActivationFactory)Marshal.GetNativeActivationFactory(type);
}
else
{
#if FEATURE_COMINTEROP_WINRT_MANAGED_ACTIVATION
return GetManagedActivationFactory(type);
#else
// Managed factories are not supported so as to minimize public surface (and test effort)
throw new NotSupportedException();
#endif
}
}
If I proceed beyond this exception it throws the following:
System.PlatformNotSupportedException occurred
HResult=-2146233031
Message=Operation is not supported on this platform.
Source=PresentationCore
StackTrace:
at MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane..ctor(Nullable`1 hwnd)
InnerException:
And a partial call stack at that point:
PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.InputPane(System.IntPtr? hwnd) Unknown
PresentationCore.dll!MS.Internal.WindowsRuntime.Windows.UI.ViewManagement.InputPane.GetForWindow(System.Windows.Interop.HwndSource source) Unknown
PresentationCore.dll!MS.Internal.Interop.TipTsfHelper.Show(System.Windows.DependencyObject focusedObject) Unknown
PresentationCore.dll!System.Windows.UIElement.Focus() Unknown
PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor This = {System.Windows.Documents.TextEditor}) Unknown
PresentationFramework.dll!System.Windows.Documents.TextEditorMouse.OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationFramework.dll!System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(System.Windows.Input.MouseButtonEventArgs e) Unknown
PresentationCore.dll!System.Windows.UIElement.OnMouseDownThunk(object sender = {System.Windows.Controls.TextBox}, System.Windows.Input.MouseButtonEventArgs e = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown
PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, bool reRaised = false) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.Grid}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown
PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown
PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input) Unknown
PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) Unknown
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) Unknown
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = {System.IntPtr}, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONDOWN, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}, ref bool handled = false) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler = null) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd = {System.IntPtr}, int msg = 513, System.IntPtr wParam = {System.IntPtr}, System.IntPtr lParam = {System.IntPtr}) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
Proceeding from there, everything runs as expected.
This problem has vanished after upgrading to Visual Studio 2017.