[Window Walker] Path for elevated process (#15186)

This commit is contained in:
Davide Giacometti
2022-01-10 16:15:39 +01:00
committed by GitHub
parent 67933a8470
commit cf0c45a319
2 changed files with 21 additions and 9 deletions

View File

@@ -645,9 +645,19 @@ namespace Microsoft.Plugin.WindowWalker.Components
/// <summary>
/// GetWindowLong index to retrieves the extended window styles.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Matching interop var")]
public const int GWL_EXSTYLE = -20;
/// <summary>
/// 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.
/// </summary>
public const int WM_SYSCOMMAND = 0x0112;
/// <summary>
/// Restores the window to its normal position and size.
/// </summary>
public const int SC_RESTORE = 0xf120;
/// <summary>
/// The following are the extended window styles
/// </summary>
@@ -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);
}
}

View File

@@ -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)