-
Notifications
You must be signed in to change notification settings - Fork 976
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
User32 ActivateKeyBoardLayout #7451
User32 ActivateKeyBoardLayout #7451
Conversation
src/System.Windows.Forms.Primitives/src/System.Windows.Forms.Primitives.csproj
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep a single NativeMethods.txt in the root for discoverability.
I performed the merge manually, I detected more zero white space and manually removed it. I suggest everyone be very careful when doing merges. |
@@ -51,7 +53,7 @@ public static InputLanguage CurrentInputLanguage | |||
value = DefaultInputLanguage; | |||
} | |||
|
|||
IntPtr handleOld = User32.ActivateKeyboardLayout(value.Handle, 0); | |||
HKL handleOld = PInvoke.ActivateKeyboardLayout(new HKL(value.Handle), 0); | |||
if (handleOld == IntPtr.Zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we still comparing it with IntPtr.Zero? can we just check == 0
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, I think the compiler doesn't like it because HKL doesn't have an implicit conversion to int? I wasn't sure what to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried this syntax. It compiled fine when targeting .NET 6 and net472:
using Windows.Win32.UI.TextServices;
HKL hkl = default;
if (hkl == 0)
{
}
This is using CsWin32 0.2.1-beta.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HKL
should have a .NULL
on it, if not get the .Value
and compare to 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HKL looks like a candidate for the IsNull
property that we're supposed to generate on pointer-sized handles, per microsoft/CsWin32#206. If HKL is a handle (or a pointer) and we missed that, can you file a new issue for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. microsoft/CsWin32#610
@@ -51,7 +53,7 @@ public static InputLanguage CurrentInputLanguage | |||
value = DefaultInputLanguage; | |||
} | |||
|
|||
IntPtr handleOld = User32.ActivateKeyboardLayout(value.Handle, 0); | |||
HKL handleOld = PInvoke.ActivateKeyboardLayout(new HKL(value.Handle), 0); | |||
if (handleOld == IntPtr.Zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried this syntax. It compiled fine when targeting .NET 6 and net472:
using Windows.Win32.UI.TextServices;
HKL hkl = default;
if (hkl == 0)
{
}
This is using CsWin32 0.2.1-beta.
var handles = new IntPtr[size]; | ||
fixed (IntPtr* pHandles = handles) | ||
var handles = new HKL[size]; | ||
fixed (HKL* pHandles = handles) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for fixed
here any more. With CsWin32 you can do this:
var handles = new HKL[5];
PInvoke.GetKeyboardLayoutList(handles);
IntPtr handleOld = User32.ActivateKeyboardLayout(value.Handle, 0); | ||
if (handleOld == IntPtr.Zero) | ||
HKL handleOld = PInvoke.ActivateKeyboardLayout(new HKL(value.Handle), 0); | ||
if (handleOld.Value == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the .Value
here shouldn't be required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird. Well, I'm glad you have a workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing a cast to nint works and there is no nint.Zero. HKL has an operator for nint, but not int. Should I just do the cast or is there a better way?
HKL handleOld = PInvoke.ActivateKeyboardLayout(new HKL(value.Handle), 0);
if (handleOld == (nint)0)
{
throw new ArgumentException(SR.ErrorBadInputLanguage, nameof(value));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cast isn't necessary in my project. Maybe it's an odd C# 11 or .NET 7 targeting thing. You can choose whatever syntax you're happiest with. They're all equivalent in the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does if (handleOld == default)
work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
if (handleOld == default)
work?
yes! Well it compiles. I don't know if its the same. Would you like me to push that through?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for integral types such as int
, IntPtr
, etc. default
== 0. So it's equivalent.
@elachlan , can you resolve merge conflict? |
Changes from review remove zero width space character changes from review use default instead of .value==0
0bb7fd8
to
70314f5
Compare
Migrate PInvokes Using CsWin32 Changes from review remove zero width space character changes from review use default instead of .value==0
#7445
Microsoft Reviewers: Open in CodeFlow