mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[cmdpal] Fix windows service extension crash issue (#38166)
repro step: change language to non-english language. stably reproducible. --------- Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
This commit is contained in:
@@ -10,7 +10,6 @@ using System.Linq;
|
|||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using Microsoft.CmdPal.Ext.WindowsServices.Commands;
|
using Microsoft.CmdPal.Ext.WindowsServices.Commands;
|
||||||
using Microsoft.CmdPal.Ext.WindowsServices.Properties;
|
using Microsoft.CmdPal.Ext.WindowsServices.Properties;
|
||||||
using Microsoft.CommandPalette.Extensions;
|
|
||||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using Windows.System;
|
using Windows.System;
|
||||||
@@ -44,9 +43,14 @@ public static class ServiceHelper
|
|||||||
serviceList = servicesStartsWith.Concat(servicesContains);
|
serviceList = servicesStartsWith.Concat(servicesContains);
|
||||||
}
|
}
|
||||||
|
|
||||||
return serviceList.Select(s =>
|
var result = serviceList.Select(s =>
|
||||||
{
|
{
|
||||||
var serviceResult = new ServiceResult(s);
|
var serviceResult = ServiceResult.CreateServiceController(s);
|
||||||
|
if (serviceResult == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
ServiceCommand serviceCommand;
|
ServiceCommand serviceCommand;
|
||||||
CommandContextItem[] moreCommands;
|
CommandContextItem[] moreCommands;
|
||||||
if (serviceResult.IsRunning)
|
if (serviceResult.IsRunning)
|
||||||
@@ -93,7 +97,9 @@ public static class ServiceHelper
|
|||||||
// ToolTipData = new ToolTipData(serviceResult.DisplayName, serviceResult.ServiceName),
|
// ToolTipData = new ToolTipData(serviceResult.DisplayName, serviceResult.ServiceName),
|
||||||
// IcoPath = icoPath,
|
// IcoPath = icoPath,
|
||||||
};
|
};
|
||||||
});
|
}).Where(s => s != null);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO GH #118 IPublicAPI contextAPI isn't used anymore, but we need equivalent ways to show notifications and status
|
// TODO GH #118 IPublicAPI contextAPI isn't used anymore, but we need equivalent ways to show notifications and status
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class ServiceResult
|
|||||||
|
|
||||||
public bool IsRunning { get; }
|
public bool IsRunning { get; }
|
||||||
|
|
||||||
public ServiceResult(ServiceController serviceController)
|
private ServiceResult(ServiceController serviceController)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(serviceController);
|
ArgumentNullException.ThrowIfNull(serviceController);
|
||||||
|
|
||||||
@@ -26,4 +26,21 @@ public class ServiceResult
|
|||||||
StartMode = serviceController.StartType;
|
StartMode = serviceController.StartType;
|
||||||
IsRunning = serviceController.Status != ServiceControllerStatus.Stopped && serviceController.Status != ServiceControllerStatus.StopPending;
|
IsRunning = serviceController.Status != ServiceControllerStatus.Stopped && serviceController.Status != ServiceControllerStatus.StopPending;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ServiceResult CreateServiceController(ServiceController serviceController)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = new ServiceResult(serviceController);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// try to log the exception in the future
|
||||||
|
// retrieve properties from serviceController will throw exception. Such as PlatformNotSupportedException.
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user