PowerToys/src/settings-ui/PowerToys.Settings/Program.cs

89 lines
2.9 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation
2020-04-08 05:19:33 +08:00
// 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.Windows;
using interop;
2020-08-18 01:00:56 +08:00
using ManagedCommon;
2020-04-08 05:19:33 +08:00
namespace PowerToys.Settings
2020-04-08 05:19:33 +08:00
{
public static class Program
2020-04-08 05:19:33 +08:00
{
private enum Arguments
{
PTPipeName = 0,
SettingsPipeName,
PTPid,
Theme, // used in the old settings
ElevatedStatus,
IsUserAdmin,
}
// Quantity of arguments
private const int ArgumentsQty = 6;
2020-04-08 05:19:33 +08:00
// Create an instance of the IPC wrapper.
private static TwoWayPipeMessageIPCManaged ipcmanager;
2020-04-08 05:19:33 +08:00
public static bool IsElevated { get; set; }
public static bool IsUserAnAdmin { get; set; }
public static int PowerToysPID { get; set; }
public static Action<string> IPCMessageReceivedCallback { get; set; }
[STAThread]
2020-04-08 05:19:33 +08:00
public static void Main(string[] args)
{
using (new Microsoft.PowerToys.Settings.UI.App())
2020-04-08 05:19:33 +08:00
{
App app = new App();
app.InitializeComponent();
if (args != null && args.Length >= ArgumentsQty)
2020-04-08 05:19:33 +08:00
{
_ = int.TryParse(args[(int)Arguments.PTPid], out int powerToysPID);
PowerToysPID = powerToysPID;
IsElevated = args[(int)Arguments.ElevatedStatus] == "true";
IsUserAnAdmin = args[(int)Arguments.IsUserAdmin] == "true";
2020-08-18 01:00:56 +08:00
RunnerHelper.WaitForPowerToysRunner(PowerToysPID, () =>
{
Environment.Exit(0);
});
ipcmanager = new TwoWayPipeMessageIPCManaged(args[(int)Arguments.SettingsPipeName], args[(int)Arguments.PTPipeName], (string message) =>
{
if (IPCMessageReceivedCallback != null && message.Length > 0)
{
Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
{
IPCMessageReceivedCallback(message);
}));
}
});
ipcmanager.Start();
2020-04-08 05:19:33 +08:00
app.Run();
}
else
{
MessageBox.Show(
"The application cannot be run as a standalone process. Please start the application through the runner.",
"Forbidden",
MessageBoxButton.OK);
app.Shutdown();
}
}
}
public static TwoWayPipeMessageIPCManaged GetTwoWayIPCManager()
2020-04-08 05:19:33 +08:00
{
return ipcmanager;
}
}
}