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

52 lines
1.9 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.Threading;
2025-11-26 16:02:41 +08:00
using System.Threading.Tasks;
2025-11-25 10:10:53 +08:00
using ManagedCommon;
2025-11-19 19:16:38 +08:00
using Microsoft.CommandPalette.Extensions;
2025-11-26 16:02:41 +08:00
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");
}
catch
{
2025-11-26 16:02:41 +08:00
// Continue even if logging fails.
2025-11-25 10:10:53 +08:00
}
2025-11-26 16:02:41 +08:00
if (args.Length > 0 && args[0] == "-RegisterProcessAsComServer")
2025-11-19 19:16:38 +08:00
{
2025-11-26 16:02:41 +08:00
using ExtensionServer server = new();
2025-11-25 15:49:50 +08:00
2025-11-26 16:02:41 +08:00
ManualResetEvent extensionDisposedEvent = new(false);
2025-11-19 19:16:38 +08:00
2025-11-26 16:02:41 +08:00
// We are instantiating an extension instance once above, and returning it every time the callback in RegisterExtension below is called.
// This makes sure that only one instance of SampleExtension is alive, which is returned every time the host asks for the IExtension object.
// If you want to instantiate a new instance each time the host asks, create the new instance inside the delegate.
PowerToysExtension extensionInstance = new(extensionDisposedEvent);
2025-11-19 19:16:38 +08:00
server.RegisterExtension(() => extensionInstance);
2025-11-26 16:02:41 +08:00
// This will make the main thread wait until the event is signalled by the extension class.
// Since we have single instance of the extension object, we exit as soon as it is disposed.
extensionDisposedEvent.WaitOne();
}
else
{
Console.WriteLine("Not being launched as a Extension... exiting.");
}
2025-11-19 19:16:38 +08:00
}
}