mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-07-04 09:30:04 +02:00
Compare commits
3 Commits
powerscrip
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bef5a3635d | ||
|
|
8107e05ad7 | ||
|
|
bc662edad4 |
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.CmdPal.Ext.WindowWalker.Components;
|
||||
using Microsoft.CmdPal.Ext.WindowWalker.Helpers;
|
||||
@@ -61,7 +62,25 @@ internal sealed partial class EndTaskCommand : InvokableCommand
|
||||
}
|
||||
|
||||
// Kill process
|
||||
window.Process.KillThisProcess(SettingsManager.Instance.KillProcessTree);
|
||||
try
|
||||
{
|
||||
window.Process.KillThisProcess(SettingsManager.Instance.KillProcessTree);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// The process already exited between the existence check and the kill attempt.
|
||||
}
|
||||
catch (Win32Exception ex)
|
||||
{
|
||||
ExtensionHost.LogMessage(new LogMessage { Message = $"Failed to kill process '{window.Process.Name}' ({window.Process.ProcessID}) of the window '{window.Title}' ({window.Hwnd}): {ex.Message}" });
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
ExtensionHost.LogMessage(new LogMessage { Message = $"Failed to kill process '{window.Process.Name}' ({window.Process.ProcessID}) of the window '{window.Title}' ({window.Hwnd}): {ex.Message}" });
|
||||
return false;
|
||||
}
|
||||
|
||||
return !SettingsManager.Instance.KeepOpenAfterKillAndClose;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// 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.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.CmdPal.Ext.WindowWalker.Commands;
|
||||
|
||||
@@ -219,14 +221,31 @@ internal sealed class WindowProcess
|
||||
/// <param name="killProcessTree">Kill process and sub processes.</param>
|
||||
internal void KillThisProcess(bool killProcessTree)
|
||||
{
|
||||
var killTree = killProcessTree ? " /t" : string.Empty;
|
||||
|
||||
if (IsFullAccessDenied)
|
||||
{
|
||||
var killTree = killProcessTree ? " /t" : string.Empty;
|
||||
ExplorerInfoResultCommand.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, ExplorerInfoResultCommand.ShellRunAsType.Administrator, true);
|
||||
if (!ExplorerInfoResultCommand.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, ExplorerInfoResultCommand.ShellRunAsType.Administrator, true))
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to start elevated taskkill for process ID {ProcessID}.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Process.GetProcessById((int)ProcessID).Kill(killProcessTree);
|
||||
try
|
||||
{
|
||||
using var process = Process.GetProcessById((int)ProcessID);
|
||||
process.Kill(killProcessTree);
|
||||
}
|
||||
catch (Win32Exception ex) when (ex.NativeErrorCode == 5)
|
||||
{
|
||||
// Error 5 = ERROR_ACCESS_DENIED
|
||||
// Fall back to elevated taskkill when direct kill is denied
|
||||
if (!ExplorerInfoResultCommand.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, ExplorerInfoResultCommand.ShellRunAsType.Administrator, true))
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to start elevated taskkill for process ID {ProcessID}.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
@@ -102,7 +103,25 @@ namespace Microsoft.Plugin.WindowWalker.Components
|
||||
}
|
||||
|
||||
// Kill process
|
||||
window.Process.KillThisProcess(WindowWalkerSettings.Instance.KillProcessTree);
|
||||
try
|
||||
{
|
||||
window.Process.KillThisProcess(WindowWalkerSettings.Instance.KillProcessTree);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// The process already exited between the existence check and the kill attempt.
|
||||
}
|
||||
catch (Win32Exception ex)
|
||||
{
|
||||
Log.Exception($"Failed to kill process '{window.Process.Name}' ({window.Process.ProcessID}) of the window '{window.Title}' ({window.Hwnd}).", ex, typeof(ContextMenuHelper));
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Log.Exception($"Failed to kill process '{window.Process.Name}' ({window.Process.ProcessID}) of the window '{window.Title}' ({window.Hwnd}).", ex, typeof(ContextMenuHelper));
|
||||
return false;
|
||||
}
|
||||
|
||||
return !WindowWalkerSettings.Instance.OpenAfterKillAndClose;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// 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.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -208,14 +209,31 @@ namespace Microsoft.Plugin.WindowWalker.Components
|
||||
/// <param name="killProcessTree">Kill process and sub processes.</param>
|
||||
internal void KillThisProcess(bool killProcessTree)
|
||||
{
|
||||
string killTree = killProcessTree ? " /t" : string.Empty;
|
||||
|
||||
if (IsFullAccessDenied)
|
||||
{
|
||||
string killTree = killProcessTree ? " /t" : string.Empty;
|
||||
Helper.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, Helper.ShellRunAsType.Administrator, true);
|
||||
if (!Helper.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, Helper.ShellRunAsType.Administrator, true))
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to start elevated taskkill for process ID {ProcessID}.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Process.GetProcessById((int)ProcessID).Kill(killProcessTree);
|
||||
try
|
||||
{
|
||||
using Process process = Process.GetProcessById((int)ProcessID);
|
||||
process.Kill(killProcessTree);
|
||||
}
|
||||
catch (Win32Exception ex) when (ex.NativeErrorCode == 5)
|
||||
{
|
||||
// Error 5 = ERROR_ACCESS_DENIED
|
||||
// Fall back to elevated taskkill when direct kill is denied
|
||||
if (!Helper.OpenInShell("taskkill.exe", $"/pid {(int)ProcessID} /f{killTree}", null, Helper.ShellRunAsType.Administrator, true))
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to start elevated taskkill for process ID {ProcessID}.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user