From cf0c45a319755e9ab7a9c9a264bd6e596197bbbd Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Mon, 10 Jan 2022 16:15:39 +0100 Subject: [PATCH] [Window Walker] Path for elevated process (#15186) --- .../Components/NativeMethods.cs | 15 ++++++++++++++- .../Components/Window.cs | 15 +++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/NativeMethods.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/NativeMethods.cs index b0a6e4355b..c158e51f1a 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/NativeMethods.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/NativeMethods.cs @@ -645,9 +645,19 @@ namespace Microsoft.Plugin.WindowWalker.Components /// /// GetWindowLong index to retrieves the extended window styles. /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Matching interop var")] public const int GWL_EXSTYLE = -20; + /// + /// A window receives this message when the user chooses a command from the Window menu (formerly known as the system or control menu) + /// or when the user chooses the maximize button, minimize button, restore button, or close button. + /// + public const int WM_SYSCOMMAND = 0x0112; + + /// + /// Restores the window to its normal position and size. + /// + public const int SC_RESTORE = 0xf120; + /// /// The following are the extended window styles /// @@ -877,5 +887,8 @@ namespace Microsoft.Plugin.WindowWalker.Components [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl); + + [DllImport("user32.dll")] + public static extern int SendMessage(IntPtr hWnd, int msg, int wParam); } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs index 7e28b738fc..59bf6471e3 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.WindowWalker/Components/Window.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -6,15 +6,10 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; -using System.Windows.Media.Imaging; namespace Microsoft.Plugin.WindowWalker.Components { @@ -259,7 +254,11 @@ namespace Microsoft.Plugin.WindowWalker.Components } else { - NativeMethods.ShowWindow(Hwnd, NativeMethods.ShowWindowCommands.Restore); + if (!NativeMethods.ShowWindow(Hwnd, NativeMethods.ShowWindowCommands.Restore)) + { + // ShowWindow doesn't work if the process is running elevated: fallback to SendMessage + _ = NativeMethods.SendMessage(Hwnd, NativeMethods.WM_SYSCOMMAND, NativeMethods.SC_RESTORE); + } } NativeMethods.FlashWindow(Hwnd, true); @@ -318,7 +317,7 @@ namespace Microsoft.Plugin.WindowWalker.Components { uint processId = GetProcessIDFromWindowHandle(hwnd); ProcessID = processId; - IntPtr processHandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.AllAccess, true, (int)processId); + IntPtr processHandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.QueryLimitedInformation, true, (int)processId); StringBuilder processName = new StringBuilder(MaximumFileNameLength); if (NativeMethods.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0)