Compare commits

...

3 Commits

Author SHA1 Message Date
Muyuan Li (from Dev Box)
1beb8a41e0 Address Copilot review: treat UAC cancel as non-error, pass exception to LogError (PR #47396)
- Catch Win32Exception with ERROR_CANCELLED (1223) separately and log at
  Debug level since user-initiated cancellation is not an error condition
- Use Logger.LogError(message, ex) overload for real failures to preserve
  stack trace, HResult, and inner exception details
- Add using System.ComponentModel for Win32Exception
2026-05-14 13:12:37 +08:00
copilot-swe-agent[bot]
0aa4a910a5 Fix CmdPal crash when running app as administrator: add exception handling in RunAsAdmin/RunAsUser
Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/3fe9efe9-4990-4c00-92b7-22b4151246dd

Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
2026-04-29 09:25:40 +00:00
copilot-swe-agent[bot]
24ecc60b69 Initial plan 2026-04-29 08:51:16 +00:00
2 changed files with 41 additions and 13 deletions

View File

@@ -3,8 +3,10 @@
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.CmdPal.Ext.Apps.Properties;
using Microsoft.CmdPal.Ext.Apps.Utils;
using Microsoft.CommandPalette.Extensions.Toolkit;
@@ -31,21 +33,33 @@ internal sealed partial class RunAsAdminCommand : InvokableCommand
{
await Task.Run(() =>
{
if (packaged)
try
{
var command = "shell:AppsFolder\\" + target;
command = Environment.ExpandEnvironmentVariables(command.Trim());
if (packaged)
{
var command = "shell:AppsFolder\\" + target;
command = Environment.ExpandEnvironmentVariables(command.Trim());
var info = ShellCommand.SetProcessStartInfo(command, verb: "runas");
info.UseShellExecute = true;
info.Arguments = string.Empty;
Process.Start(info);
var info = ShellCommand.SetProcessStartInfo(command, verb: "runas");
info.UseShellExecute = true;
info.Arguments = string.Empty;
Process.Start(info);
}
else
{
var info = ShellCommand.GetProcessStartInfo(target, parentDir, string.Empty, ShellCommand.RunAsType.Administrator);
Process.Start(info);
}
}
else
catch (Win32Exception ex) when (ex.NativeErrorCode == 1223)
{
var info = ShellCommand.GetProcessStartInfo(target, parentDir, string.Empty, ShellCommand.RunAsType.Administrator);
Process.Start(info);
// ERROR_CANCELLED: user dismissed the UAC prompt — not an error
Logger.LogDebug("Run as administrator cancelled by user.");
}
catch (Exception ex)
{
Logger.LogError("Failed to run as administrator", ex);
}
});
}

View File

@@ -3,8 +3,10 @@
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.CmdPal.Ext.Apps.Properties;
using Microsoft.CmdPal.Ext.Apps.Utils;
using Microsoft.CommandPalette.Extensions.Toolkit;
@@ -29,9 +31,21 @@ internal sealed partial class RunAsUserCommand : InvokableCommand
{
await Task.Run(() =>
{
var info = ShellCommand.GetProcessStartInfo(target, parentDir, string.Empty, ShellCommand.RunAsType.OtherUser);
try
{
var info = ShellCommand.GetProcessStartInfo(target, parentDir, string.Empty, ShellCommand.RunAsType.OtherUser);
Process.Start(info);
Process.Start(info);
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 1223)
{
// ERROR_CANCELLED: user dismissed the UAC/credential prompt — not an error
Logger.LogDebug("Run as different user cancelled by user.");
}
catch (Exception ex)
{
Logger.LogError("Failed to run as different user", ex);
}
});
}