Arithmetic operation resulted in an overflow in InputLanguageChangingEventArgs

你。 提交于 2019-12-25 06:01:19

问题


I am having an issue with a user where my application just throws and unhandled exception for no particular reason. I am not sure what is causing this as the application itself does not check nor handle anything remotely related to and Input Language change event. The exception is pretty vague as it stands with no inner exception or any other information that tells what is going on as it seems to be an arithmetic overflow exception.

Here is the exception message and stack trace:

Type:        System.OverflowException
Message:     算術演算の結果オーバーフローが発生しました。
Source:      System.Windows.Forms
Stack Trace: 場所 System.Windows.Forms.InputLanguageChangingEventArgs..ctor(InputLanguage inputLanguage, Boolean sysCharSet)
   場所 System.Windows.Forms.Control.WmInputLangChangeRequest(Message& m)
   場所 System.Windows.Forms.Control.WndProc(Message& m)
   場所 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   場所 System.Windows.Forms.Button.WndProc(Message& m)
   場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The exception message is "Arithmetic operation resulted in an overflow". Has anyone experienced such behaviour before


回答1:


This is actually a bug in .NET Framework, in System.Windows.Forms.InputLanguage.Culture getter (you cannot see it in the stack trace since it gets inlined inside InputLanguageChangingEventArgs..ctor by the JIT):

public CultureInfo Culture
{
    get
    {
        return new CultureInfo((int)this.handle & 65535);
    }
}

here, this.handle is IntPtr and that means it is 64-bit on x64 OS, but mistakenly casted to int, and that causes the OverflowException if some of the higher bits in this handle are set.

The only workaround I can think of is to completely filter out messages with handle not fitting into int type:

// call this before Application.Run():
Application.AddMessageFilter(new WmInputLangChangeRequestFilter());

class WmInputLangChangeRequestFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x0050)
        {
            return (long)m.LParam > 0x7FFFFFFF;
        }
        return false;
    }
}



回答2:


I have seen the "OverflowException" in Greenshot, where I researched this I arrived here.

Here is some additional information to the issue: On MSDN there are some comments to it: https://msdn.microsoft.com/en-us/library/windows/desktop/ms632630(v=vs.85).aspx It is said that the Windows-Message is not posted on e.g. Windows 7, which I can confirm I have never seen it and can't reproduce it.

Here is also some information "WHAT BROKE THE INPUT LANGUAGE MESSAGES?": http://www.siao2.com/2006/05/16/598980.aspx

I currently think that for my application the message is not important, so I added Torvin's solution to ignore it...



来源:https://stackoverflow.com/questions/25619831/arithmetic-operation-resulted-in-an-overflow-in-inputlanguagechangingeventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!