Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception:算术运算导致溢出。 #886

Closed
cuiliang opened this issue Jun 28, 2021 · 5 comments
Closed

Exception:算术运算导致溢出。 #886

cuiliang opened this issue Jun 28, 2021 · 5 comments

Comments

@cuiliang
Copy link
Contributor

这不是一个HC的bug,但是应该可以在HC里加以规避处理。

现象
在个别人的电脑上会出现一个奇怪的异常:

OverflowException
   在 System.Windows.Shell.WindowChromeWorker._HandleNCHitTest(WM uMsg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 System.Windows.Shell.WindowChromeWorker._WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   在 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)

相关信息
在StackOverflow上找到一个相关的帖子:
https://stackoverflow.com/questions/33287542/what-would-cause-wm-nchittest-lparam-to-overflow-a-32-bit-integer

里面有人贴了一个一个网址:
https://developercommunity.visualstudio.com/t/overflow-exception-in-windowchrome/167357

提到的解决方法:

protected override void OnSourceInitialized( EventArgs e )
      {
         base.OnSourceInitialized( e );
         ( (HwndSource)PresentationSource.FromVisual( this ) ).AddHook( HookProc );
      }
      private IntPtr HookProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
      {
         if ( msg == 0x0084 /*WM_NCHITTEST*/ )
         {
            // This prevents a crash in WindowChromeWorker._HandleNCHitTest
            try
            {
               lParam.ToInt32();
            }
            catch ( OverflowException )
            {
               handled = true;
            }
         }
         return IntPtr.Zero;
      }

其它相关链接:

@NaBian
Copy link
Member

NaBian commented Jun 28, 2021

重写 OnSourceInitialized 方法确认过可以解决这个问题吗?

@cuiliang
Copy link
Contributor Author

看起来是可以的。目前hc:Window类里已经有HookProc,我觉得加上这个判断应该不会产生额外的负担却可以避免崩溃,还是值得的。

@NaBian
Copy link
Member

NaBian commented Jun 28, 2021

ok

@lindexi
Copy link
Contributor

lindexi commented Jul 12, 2022

我感觉这是应该在 WPF 框架处理的,我准备在底层处理

@dlgcy
Copy link

dlgcy commented Aug 8, 2024

我感觉这是应该在 WPF 框架处理的,我准备在底层处理

在哪个版本的 .NET 中修复了?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants