Files
PowerToys/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Program.cs

71 lines
2.4 KiB
C#
Raw Normal View History

2025-11-19 19:16:38 +08:00
// 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.
using System;
using System.Runtime.InteropServices;
2025-11-19 19:16:38 +08:00
using System.Threading;
2025-11-25 10:10:53 +08:00
using ManagedCommon;
2025-11-19 19:16:38 +08:00
using Microsoft.CommandPalette.Extensions;
using Shmuelie.WinRTServer;
using Shmuelie.WinRTServer.CsWinRT;
2025-11-19 19:16:38 +08:00
2025-11-26 16:02:41 +08:00
namespace PowerToysExtension;
2025-11-19 19:16:38 +08:00
2025-11-26 16:02:41 +08:00
public class Program
2025-11-19 19:16:38 +08:00
{
[MTAThread]
2025-11-26 16:02:41 +08:00
public static void Main(string[] args)
2025-11-19 19:16:38 +08:00
{
2025-11-25 10:10:53 +08:00
try
{
2025-11-26 16:02:41 +08:00
// Initialize per-extension log under CmdPal/PowerToysExtension.
2025-11-25 10:10:53 +08:00
Logger.InitializeLogger("\\CmdPal\\PowerToysExtension\\Logs");
Logger.LogInfo($"PowerToysExtension starting. Args=\"{string.Join(' ', args)}\" ProcArch={RuntimeInformation.ProcessArchitecture} OSArch={RuntimeInformation.OSArchitecture} BaseDir={AppContext.BaseDirectory}");
2025-11-25 10:10:53 +08:00
}
catch
{
2025-11-26 16:02:41 +08:00
// Continue even if logging fails.
2025-11-25 10:10:53 +08:00
}
2025-11-27 09:21:55 +08:00
try
2025-11-19 19:16:38 +08:00
{
2025-11-27 09:21:55 +08:00
if (args.Length > 0 && args[0] == "-RegisterProcessAsComServer")
{
Logger.LogInfo("RegisterProcessAsComServer mode detected.");
ComServer server = new();
2025-11-27 09:21:55 +08:00
ManualResetEvent extensionDisposedEvent = new(false);
try
{
PowerToysExtension extensionInstance = new(extensionDisposedEvent);
Logger.LogInfo("Registering extension via Shmuelie.WinRTServer.");
server.RegisterClass<PowerToysExtension, IExtension>(() => extensionInstance);
server.Start();
Logger.LogInfo("Extension instance registered; waiting for disposal signal.");
2025-11-19 19:16:38 +08:00
extensionDisposedEvent.WaitOne();
Logger.LogInfo("Extension disposed signal received; exiting server loop.");
}
finally
{
server.Stop();
server.UnsafeDispose();
}
2025-11-27 09:21:55 +08:00
}
else
{
Console.WriteLine("Not being launched as a Extension... exiting.");
Logger.LogInfo("Exited: not launched with -RegisterProcessAsComServer.");
}
2025-11-26 16:02:41 +08:00
}
catch (Exception ex)
{
Logger.LogError("Unhandled exception in PowerToysExtension.Main", ex);
throw;
}
2025-11-27 09:21:55 +08:00
finally
2025-11-26 16:02:41 +08:00
{
}
2025-11-19 19:16:38 +08:00
}
}