Repository navigation
User32 ActivateKeyBoardLayout - #7451
Conversation
JeremyKuhne
left a comment
There was a problem hiding this comment.
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. |
|
|
||
| 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.
should we still comparing it with IntPtr.Zero? can we just check == 0 here?
There was a problem hiding this comment.
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.
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.
HKL should have a .NULL on it, if not get the .Value and compare to 0.
There was a problem hiding this comment.
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?
|
|
||
| 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.
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.
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.
nit: the .Value here shouldn't be required.
There was a problem hiding this comment.
Weird. Well, I'm glad you have a workaround.
There was a problem hiding this comment.
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.
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.
Does if (handleOld == default) work?
There was a problem hiding this comment.
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.
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