From a9f7c3bd50242a06d223273a4f4235c67207c606 Mon Sep 17 00:00:00 2001 From: Yu Leng <42196638+moooyo@users.noreply.github.com> Date: Tue, 1 Jul 2025 10:36:59 +0800 Subject: [PATCH] [cmdpal][AOT] fix some cmdpal aot issue (#40301) ## Summary of the Pull Request Most of them are safe change but only one line may break something is in [TrayIconService.cs](https://github.com/microsoft/PowerToys/compare/yuleng/aot/pr?expand=1#diff-bc3e603a50df89710c69ff6f4fc8f29967fca19cdbe615f06b53534e9c986bb5) ok, I've confirmed no problem. ## PR Checklist - [ ] **Closes:** #xxx - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed Co-authored-by: Yu Leng (from Dev Box) --- .../Models/ExtensionWrapper.cs | 2 +- .../HotkeySettingsControlHook.cs | 2 +- .../ShortcutControl/NativeKeyboardHelper.cs | 143 +++++++++--------- .../Controls/ShortcutControl/NativeMethods.cs | 10 +- .../Helpers/TrayIconService.cs | 4 +- .../Microsoft.CmdPal.UI/NativeMethods.txt | 5 - 6 files changed, 80 insertions(+), 86 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs index 2a6d7c65c1..5b4eee44ef 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs @@ -121,7 +121,7 @@ public class ExtensionWrapper : IExtensionWrapper if (hr.Value == -2147024893) { - Logger.LogDebug($"Failed to find {ExtensionDisplayName}: {hr}. It may have been uninstalled or deleted."); + Logger.LogError($"Failed to find {ExtensionDisplayName}: {hr}. It may have been uninstalled or deleted."); // We don't really need to throw this exception. // We'll just return out nothing. diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/HotkeySettingsControlHook.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/HotkeySettingsControlHook.cs index 66f482f502..6ea1c708a7 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/HotkeySettingsControlHook.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/HotkeySettingsControlHook.cs @@ -12,7 +12,7 @@ public delegate bool IsActive(); public delegate bool FilterAccessibleKeyboardEvents(int key, UIntPtr extraInfo); -public class HotkeySettingsControlHook : IDisposable +public partial class HotkeySettingsControlHook : IDisposable { private const int WmKeyDown = 0x100; private const int WmKeyUp = 0x101; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeKeyboardHelper.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeKeyboardHelper.cs index 9d3961f907..48b02c4412 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeKeyboardHelper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeKeyboardHelper.cs @@ -5,82 +5,81 @@ using System; using System.Runtime.InteropServices; -namespace Microsoft.PowerToys.Settings.UI.Helpers +namespace Microsoft.PowerToys.Settings.UI.Helpers; + +internal static class NativeKeyboardHelper { - internal static class NativeKeyboardHelper + [StructLayout(LayoutKind.Sequential)] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] + internal struct INPUT { - [StructLayout(LayoutKind.Sequential)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] - internal struct INPUT - { - internal INPUTTYPE type; - internal InputUnion data; + internal INPUTTYPE type; + internal InputUnion data; - internal static int Size - { - get { return Marshal.SizeOf(typeof(INPUT)); } - } - } - - [StructLayout(LayoutKind.Explicit)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] - internal struct InputUnion + internal static int Size { - [FieldOffset(0)] - internal MOUSEINPUT mi; - [FieldOffset(0)] - internal KEYBDINPUT ki; - [FieldOffset(0)] - internal HARDWAREINPUT hi; - } - - [StructLayout(LayoutKind.Sequential)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] - internal struct MOUSEINPUT - { - internal int dx; - internal int dy; - internal int mouseData; - internal uint dwFlags; - internal uint time; - internal UIntPtr dwExtraInfo; - } - - [StructLayout(LayoutKind.Sequential)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] - internal struct KEYBDINPUT - { - internal short wVk; - internal short wScan; - internal uint dwFlags; - internal int time; - internal UIntPtr dwExtraInfo; - } - - [StructLayout(LayoutKind.Sequential)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] - internal struct HARDWAREINPUT - { - internal int uMsg; - internal short wParamL; - internal short wParamH; - } - - internal enum INPUTTYPE : uint - { - INPUT_MOUSE = 0, - INPUT_KEYBOARD = 1, - INPUT_HARDWARE = 2, - } - - [Flags] - internal enum KeyEventF - { - KeyDown = 0x0000, - ExtendedKey = 0x0001, - KeyUp = 0x0002, - Unicode = 0x0004, - Scancode = 0x0008, + get { return Marshal.SizeOf(); } } } + + [StructLayout(LayoutKind.Explicit)] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] + internal struct InputUnion + { + [FieldOffset(0)] + internal MOUSEINPUT mi; + [FieldOffset(0)] + internal KEYBDINPUT ki; + [FieldOffset(0)] + internal HARDWAREINPUT hi; + } + + [StructLayout(LayoutKind.Sequential)] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] + internal struct MOUSEINPUT + { + internal int dx; + internal int dy; + internal int mouseData; + internal uint dwFlags; + internal uint time; + internal UIntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] + internal struct KEYBDINPUT + { + internal short wVk; + internal short wScan; + internal uint dwFlags; + internal int time; + internal UIntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching Native Structure")] + internal struct HARDWAREINPUT + { + internal int uMsg; + internal short wParamL; + internal short wParamH; + } + + internal enum INPUTTYPE : uint + { + INPUT_MOUSE = 0, + INPUT_KEYBOARD = 1, + INPUT_HARDWARE = 2, + } + + [Flags] + internal enum KeyEventF + { + KeyDown = 0x0000, + ExtendedKey = 0x0001, + KeyUp = 0x0002, + Unicode = 0x0004, + Scancode = 0x0008, + } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeMethods.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeMethods.cs index f89b11391b..38a50a0b94 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeMethods.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ShortcutControl/NativeMethods.cs @@ -7,7 +7,7 @@ using System.Text; namespace Microsoft.PowerToys.Settings.UI.Helpers; -public static class NativeMethods +public static partial class NativeMethods { private const int WS_POPUP = 1 << 31; // 0x80000000 internal const int GWL_STYLE = -16; @@ -26,11 +26,11 @@ public static class NativeMethods [DllImport("user32.dll")] internal static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl); - [DllImport("user32.dll")] - internal static extern uint SendInput(uint nInputs, NativeKeyboardHelper.INPUT[] pInputs, int cbSize); + [LibraryImport("user32.dll")] + internal static partial uint SendInput(uint nInputs, NativeKeyboardHelper.INPUT[] pInputs, int cbSize); - [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] - internal static extern short GetAsyncKeyState(int vKey); + [LibraryImport("user32.dll")] + internal static partial short GetAsyncKeyState(int vKey); [DllImport("user32.dll", SetLastError = true)] internal static extern int GetWindowLong(IntPtr hWnd, int nIndex); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TrayIconService.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TrayIconService.cs index 3aae253512..df72b56ef7 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TrayIconService.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TrayIconService.cs @@ -72,7 +72,7 @@ internal sealed partial class TrayIconService _largeIcon = GetAppIconHandle(); _trayIconData = new NOTIFYICONDATAW() { - cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONDATAW)), + cbSize = (uint)Marshal.SizeOf(), hWnd = _hwnd, uID = MY_NOTIFY_ID, uFlags = NOTIFY_ICON_DATA_FLAGS.NIF_MESSAGE | NOTIFY_ICON_DATA_FLAGS.NIF_ICON | NOTIFY_ICON_DATA_FLAGS.NIF_TIP, @@ -133,7 +133,7 @@ internal sealed partial class TrayIconService private DestroyIconSafeHandle GetAppIconHandle() { - var exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; + var exePath = Path.Combine(AppContext.BaseDirectory, "Microsoft.CmdPal.UI.exe"); DestroyIconSafeHandle largeIcon; PInvoke.ExtractIconEx(exePath, 0, out largeIcon, out _, 1); return largeIcon; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt b/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt index 427742f95f..35fafb4680 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt @@ -1,7 +1,6 @@ GetPhysicallyInstalledSystemMemory GlobalMemoryStatusEx GetSystemInfo -CoCreateInstance GetForegroundWindow SetForegroundWindow GetWindowRect @@ -21,10 +20,7 @@ SetActiveWindow MonitorFromWindow GetMonitorInfo GetDpiForMonitor -SHCreateStreamOnFileEx CoAllowSetForegroundWindow -SHCreateStreamOnFileEx -SHLoadIndirectString WM_HOTKEY WM_NCLBUTTONDBLCLK @@ -33,7 +29,6 @@ LoadIcon WM_USER WM_WINDOWPOSCHANGING RegisterWindowMessageW -GetModuleHandleW ExtractIconEx TRACK_POPUP_MENU_FLAGS WM_COMMAND