Skip to content

Commit

Permalink
[PT Run] Closing PT Run and new settings app when PowerToys is not ru…
Browse files Browse the repository at this point in the history
…nning (#3853)

Closing PowerLauncher, FZ Editor and new settings app when PowerToys is not running
  • Loading branch information
yevhenii44-zz committed Jun 10, 2020
1 parent 38350a1 commit 6fdfd3b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/action_runner/action_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

std::wstring_view target;
std::wstring_view pidFile;
std::wstring params;

while (nextArg < nArgs)
{
Expand All @@ -207,6 +208,7 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
}
else
{
params = args[nextArg];
nextArg++;
}
}
Expand All @@ -227,7 +229,7 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
}
}

run_same_elevation(target.data(), L"", pidBuffer);
run_same_elevation(target.data(), params, pidBuffer);

// cleanup
if (!pidFile.empty())
Expand Down
32 changes: 32 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using interop;
using Windows.UI.Popups;
Expand All @@ -21,6 +24,8 @@ public class Program

public static bool IsUserAnAdmin { get; set; }

public static int PowerToysPID { get; set; }

[STAThread]
public static void Main(string[] args)
{
Expand All @@ -31,6 +36,9 @@ public static void Main(string[] args)

if (args.Length >= ArgumentsQty)
{
int.TryParse(args[2], out int powerToysPID);
PowerToysPID = powerToysPID;

if (args[4] == "true")
{
IsElevated = true;
Expand All @@ -49,6 +57,8 @@ public static void Main(string[] args)
IsUserAnAdmin = false;
}

WaitForPowerToysRunner();

ipcmanager = new TwoWayPipeMessageIPCManaged(args[1], args[0], null);
ipcmanager.Start();
app.Run();
Expand All @@ -68,5 +78,27 @@ public static TwoWayPipeMessageIPCManaged GetTwoWayIPCManager()
{
return ipcmanager;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

internal static void WaitForPowerToysRunner()
{
Task.Run(() =>
{
const uint INFINITE = 0xFFFFFFFF;
const uint WAIT_OBJECT_0 = 0x00000000;
const uint SYNCHRONIZE = 0x00100000;
IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, PowerToysPID);
if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
{
Environment.Exit(0);
}
});
}
}
}
27 changes: 27 additions & 0 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using FancyZonesEditor.Models;

Expand All @@ -21,6 +24,8 @@ public App()

private void OnStartup(object sender, StartupEventArgs e)
{
WaitForPowerToysRunner();

LayoutModel foundModel = null;

foreach (LayoutModel model in ZoneSettings.DefaultModels)
Expand Down Expand Up @@ -57,5 +62,27 @@ private void OnStartup(object sender, StartupEventArgs e)
overlay.Show();
overlay.DataContext = foundModel;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

private void WaitForPowerToysRunner()
{
Task.Run(() =>
{
const uint INFINITE = 0xFFFFFFFF;
const uint WAIT_OBJECT_0 = 0x00000000;
const uint SYNCHRONIZE = 0x00100000;
IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, Settings.PowerToysPID);
if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
{
Environment.Exit(0);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private enum CmdArgs
ActiveZoneSetTmpFile,
AppliedZoneSetTmpFile,
CustomZoneSetsTmpFile,
PowerToysPID,
}

private static CanvasLayoutModel _blankCustomModel;
Expand Down Expand Up @@ -242,6 +243,13 @@ public static string CustomZoneSetsTmpFile

private static string _customZoneSetsTmpFile;

public static int PowerToysPID
{
get { return _powerToysPID; }
}

private static int _powerToysPID;

// UpdateLayoutModels
// Update the five default layouts based on the new ZoneCount
private void UpdateLayoutModels()
Expand Down Expand Up @@ -410,7 +418,7 @@ private void ParseCommandLineArgs()
WorkArea = SystemParameters.WorkArea;

string[] args = Environment.GetCommandLineArgs();
if (args.Length == 5)
if (args.Length == 6)
{
var parsedLocation = args[(int)CmdArgs.X_Y_Width_Height].Split('_');
var x = int.Parse(parsedLocation[0]);
Expand All @@ -424,6 +432,8 @@ private void ParseCommandLineArgs()
_appliedZoneSetTmpFile = args[(int)CmdArgs.AppliedZoneSetTmpFile];
_customZoneSetsTmpFile = args[(int)CmdArgs.CustomZoneSetsTmpFile];

int.TryParse(args[(int)CmdArgs.PowerToysPID], out _powerToysPID);

ParseDeviceInfoData();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ void FancyZones::ToggleEditor() noexcept
/*1*/ editorLocation + L" " +
/*2*/ L"\"" + ZoneWindowUtils::GetActiveZoneSetTmpPath() + L"\" " +
/*3*/ L"\"" + ZoneWindowUtils::GetAppliedZoneSetTmpPath() + L"\" " +
/*4*/ L"\"" + ZoneWindowUtils::GetCustomZoneSetsTmpPath() + L"\"";
/*4*/ L"\"" + ZoneWindowUtils::GetCustomZoneSetsTmpPath() + L"\" " +
/*5*/ L"\"" + std::to_wstring(GetCurrentProcessId()) + L"\"";

SHELLEXECUTEINFO sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
Expand Down
7 changes: 7 additions & 0 deletions src/modules/launcher/Microsoft.Launcher/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,18 @@ class Microsoft_Launcher : public PowertoyModuleIface {
// Enable the powertoy
virtual void enable()
{
unsigned long powertoys_pid = GetCurrentProcessId();

if (!is_process_elevated(false))
{
std::wstring executable_args = L"";
executable_args.append(std::to_wstring(powertoys_pid));

SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = L"modules\\launcher\\PowerLauncher.exe";
sei.nShow = SW_SHOWNORMAL;
sei.lpParameters = executable_args.data();
ShellExecuteExW(&sei);

m_hProcess = sei.hProcess;
Expand All @@ -151,6 +157,7 @@ class Microsoft_Launcher : public PowertoyModuleIface {
params += L"-target modules\\launcher\\PowerLauncher.exe ";
params += L"-pidFile ";
params += POWER_LAUNCHER_PID_SHARED_FILE;
params += L" " + std::to_wstring(powertoys_pid) + L" ";

action_runner_path += L"\\action_runner.exe";
// Set up the shared file from which to retrieve the PID of PowerLauncher
Expand Down
32 changes: 29 additions & 3 deletions src/modules/launcher/PowerLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.PowerToys.Telemetry;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
Expand All @@ -20,13 +21,12 @@

namespace PowerLauncher
{


public partial class App : IDisposable, ISingleInstanceApp
{
public static PublicAPIInstance API { get; private set; }
private const string Unique = "PowerLauncher_Unique_Application_Mutex";
private static bool _disposed;
private static int _powerToysPid;
private Settings _settings;
private MainViewModel _mainVM;
private SettingWindowViewModel _settingsVM;
Expand All @@ -35,10 +35,12 @@ public partial class App : IDisposable, ISingleInstanceApp
private SettingsWatcher _settingsWatcher;

[STAThread]
public static void Main()
public static void Main(string[] args)
{
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
int.TryParse(args[0], out _powerToysPid);

using (var application = new App())
{
application.InitializeComponent();
Expand All @@ -49,6 +51,8 @@ public static void Main()

private void OnStartup(object sender, StartupEventArgs e)
{
WaitForPowerToysRunner();

var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start();
Stopwatch.Normal("|App.OnStartup|Startup cost", () =>
Expand Down Expand Up @@ -113,6 +117,28 @@ private void RegisterExitEvents()
Current.SessionEnding += (s, e) => Dispose();
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

private void WaitForPowerToysRunner()
{
Task.Run(() =>
{
const uint INFINITE = 0xFFFFFFFF;
const uint WAIT_OBJECT_0 = 0x00000000;
const uint SYNCHRONIZE = 0x00100000;
IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, _powerToysPid);
if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
{
Environment.Exit(0);
}
});
}

/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
Expand Down

0 comments on commit 6fdfd3b

Please sign in to comment.