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 on keyboard layout switch with InvariantGlobalization #9191

Closed
OwnageIsMagic opened this issue May 27, 2023 · 15 comments
Closed

Exception on keyboard layout switch with InvariantGlobalization #9191

OwnageIsMagic opened this issue May 27, 2023 · 15 comments
Assignees

Comments

@OwnageIsMagic
Copy link

.NET version

Microsoft.WindowsDesktop.App 7.0.5

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

Unhandled exception. System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'culture')
1049 (0x0419) is an invalid culture identifier.
   at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride)
   at System.Windows.Forms.InputLanguage.get_Culture()
   at System.Windows.Forms.InputLanguageChangedEventArgs..ctor(InputLanguage inputLanguage, Byte charSet)
   at System.Windows.Forms.InputLanguage.CreateInputLanguageChangedEventArgs(Message m)
   at System.Windows.Forms.Control.WmInputLangChange(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

Steps to reproduce

@OwnageIsMagic OwnageIsMagic added the untriaged The team needs to look at this issue in the next triage label May 27, 2023
@elachlan
Copy link
Contributor

@Olina-Zhang is there enough information for your team to go off of here for repro?

@OwnageIsMagic
Copy link
Author

OwnageIsMagic commented May 28, 2023

@elachlan here is repro

dotnet new winforms -o winformsInvariantRepro && cd winformsInvariantRepro
dotnet build /p:InvariantGlobalization=true && dotnet run --no-build
# change input language

confirmed for Microsoft.WindowsDesktop.App 8.0.0-preview.3.23178.1

@Olina-Zhang
Copy link
Member

Olina-Zhang commented May 29, 2023

@OwnageIsMagic verified .Net 7.0 and 8.0 using above CLI, no exception, did you use some winforms controls?
image

@OwnageIsMagic
Copy link
Author

OwnageIsMagic commented May 29, 2023

@Olina-Zhang add additional input language to Windows here ms-settings:regionlanguage (Add a keyboard)
press Shift + Alt or Shift + Ctrl or Win + Space while Form1 is focused.
img

@OwnageIsMagic
Copy link
Author

@elachlan problem is obvious from stacktrace provided
Control.WmInputLangChange(ref Message m) creates InputLanguageChangedEventArgs

public InputLanguageChangedEventArgs(InputLanguage inputLanguage, byte charSet)
{
InputLanguage = inputLanguage.OrThrowIfNull();
Culture = inputLanguage.Culture;

calls InputLanguage.Culture which passing LCID to CultureInfo .ctor
public CultureInfo Culture => new CultureInfo(PARAM.LOWORD(_handle));

that ends up here
https://github.com/dotnet/runtime/blob/73cb995cff35b053d5b0d3daf319ac33769657d0/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs#L860-L872

@elachlan
Copy link
Contributor

@DJm00n is your PR touching this stuff?

@DJm00n
Copy link
Contributor

DJm00n commented May 29, 2023

@elachlan no, it shouldn’t. It seems related to this breaking change. I can include a fix into my PR and return InvariantCulture from InputLanguage.Culture in globalization-invariant mode.

@OwnageIsMagic
Copy link
Author

OwnageIsMagic commented May 29, 2023

@elachlan the line is mostly the same since 2018
https://github.com/dotnet/winforms/blame/a7ca739d35a6369a10d50fb3d7e39c2aeb9dd620/src/System.Windows.Forms/src/System/Windows/Forms/InputLanguage.cs#L43
The problem is that InvariantGlobalization need special handling: with it enabled there is only 1 predefined CultureInfo instance (CultureInfo.Invariant)
possible workaround is to guard InputLanguage.Culture with check for InvariantGlobalization.

@OwnageIsMagic
Copy link
Author

dotnet/runtime#81429

@elachlan
Copy link
Contributor

@OwnageIsMagic thanks for the explaination.

@DJm00n if your code is touching that area, then it might be a good idea to fix it whilst you are working on it.

@OwnageIsMagic

This comment was marked as resolved.

@elachlan
Copy link
Contributor

@OwnageIsMagic thanks! Yeah I am just a community member. Feel free to link the opened issue here.

@OwnageIsMagic
Copy link
Author

@elachlan @DJm00n This issue can now be worked around by setting <PredefinedCulturesOnly>false</PredefinedCulturesOnly>.
On WinForms side we can lazy init this property, since we store InputLanguage from which this property value comes.
It's not used internally.
https://source.dot.net/#System.Windows.Forms/System/Windows/Forms/InputLanguageChangedEventArgs.cs,5bdc3016f89c5e99,references

@merriemcgaw
Copy link
Member

@OwnageIsMagic and @elachlan you guys did all the work for us 😸 . Thanks a ton!

@ghost ghost removed the untriaged The team needs to look at this issue in the next triage label Jun 7, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants