-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Launch elevated instances via shell:AppFolder #14637
Conversation
Fixes microsoft#14501 This uses `shell:AppsFolder` to launch elevated instances of the app via `ShellExecuteEx` and `runas` in elevate-shim.exe. The app to launch is discovered via the `GetCurrentApplicationUserModelId` API. e.g. `shell:AppsFolder\WindowsTerminalDev_8wekyb3d8bbwe!App` This will fallback to launching `WindowsTerminal.exe` if it fails to discover the app user model id to launch. This also fixes an innocuous bug in elevate-shim where the first argument of WinMain was lost (e.g. `new-tab`).
@microsoft-github-policy-service agree |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'm not particularly attached to the specifics of the implementation; it was an iterative process to get here.
edit: change was moved to elevate-shim; no need to parse the cmdline It also wasn't clear if terminal is always an AppX package, in which case the fallback to invoking WindowsTerminal.exe isn't necessary. |
Reverts the changes to AppLogic.* Minimizes scope of changes to TerminalPage and elevate-shim Use WIL for logging and win32 api interaction
This comment has been minimized.
This comment has been minimized.
Just had an idea while working on something entirely unrelated. Turns out that |
seInfo.lpFile = module.c_str(); // This is `...\WindowsTerminal.exe` | ||
seInfo.lpParameters = pCmdLine; // This is `new-tab -p {guid}` | ||
seInfo.lpFile = cmd.c_str(); // This is `shell:AppsFolder\...` or `...\WindowsTerminal.exe` | ||
seInfo.lpParameters = GetCommandLine(); // This is `new-tab -p {guid}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the previous pCmdLine
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found an innocuous bug in how the first parameter, new-tab
, was dropped when passed to elevate-shim.exe by wWinMain given how it's launched by CreateProcess
. It appears wWinMain is assuming param zero is the executable path even if it's not.
However, the elevate-shim process is launched with a command line that doesn't include the executable path.
terminal/src/cascadia/TerminalApp/TerminalPage.cpp
Lines 4118 to 4135 in 8e04169
auto cmdline{ | |
fmt::format(L"new-tab {}", newTerminalArgs.ToCommandline().c_str()) | |
}; | |
wil::unique_process_information pi; | |
STARTUPINFOW si{}; | |
si.cb = sizeof(si); | |
LOG_IF_WIN32_BOOL_FALSE(CreateProcessW(exePath.c_str(), | |
cmdline.data(), | |
nullptr, | |
nullptr, | |
FALSE, | |
0, | |
nullptr, | |
nullptr, | |
&si, | |
&pi)); |
This is the !peb from elevate-shim.exe when launched from a dev build of terminal as an appx (I setup Windbg Preview as the post-mortem debugger and put a DebugBreak() at the top of elevate-shim to break in easily). Notice the CommandLine
is missing the executable filepath as the first argument. You can also see this in Process Monitor.
CurrentDirectory: 'C:\windows\system32\'
WindowTitle: 'C:\source\github.com.jboelter.terminal\src\cascadia\CascadiaPackage\bin\x64\Debug\AppX\elevate-shim.exe'
ImageFile: 'C:\source\github.com.jboelter.terminal\src\cascadia\CascadiaPackage\bin\x64\Debug\AppX\elevate-shim.exe'
CommandLine: ' new-tab --profile "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}"'
DllPath: 'C:\source\github.com.jboelter.terminal\src\cascadia\CascadiaPackage\bin\x64\Debug\AppX;C:\Program Files\WindowsApps\Microsoft.VCLibs.140.00.Debug.UWPDesktop_14.0.30704.0_x64__8wekyb3d8bbwe;'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeeeeeeeep that's totally on me. We probably could fix that down in TerminalPage.cpp, but 🤷
Is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every day I'm amazed and horrified. Let's give this a shot for 1.17. Somehow I have a feeling that this is going to be way more reliable, based on the fact that it's 500% crazier than what we had before 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for doing this. It's clever and thoughtfully-implemented 😄
Hello @DHowett! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
I know now (thanks to jboelter!) that PowerToys uses it, but I don't know whether it's been publicly documented. That's something we should definitely pursue. Thank you :) |
🎉 Handy links: |
This uses `shell:AppsFolder` to launch elevated instances of the app via `ShellExecuteEx` and `runas` in elevate-shim.exe. The app to launch is discovered via the `GetCurrentApplicationUserModelId` API. e.g. `shell:AppsFolder\WindowsTerminalDev_8wekyb3d8bbwe!App` This will fallback to launching `WindowsTerminal.exe` if it fails to discover the app user model id to launch. This also fixes a bug in elevate-shim where the first argument of WinMain was lost (e.g. `new-tab`). Curiously, `AppLogic::RunAsUwp()` is never called and `AppLogic::IsUwp()` is always false when running debug builds locally (e.g. WindowsTerminalDev). It's not clear if this is an artifact of development packages or something else. ## Validation Steps Performed Various manual debug/execution scenarios. Verified the fallback path by running the unbundled app by extracting the `CascadiaPackage_0.0.1.0_x64.msix` from the 'drop' build artifact. Fixes #14501
This uses `shell:AppsFolder` to launch elevated instances of the app via `ShellExecuteEx` and `runas` in elevate-shim.exe. The app to launch is discovered via the `GetCurrentApplicationUserModelId` API. e.g. `shell:AppsFolder\WindowsTerminalDev_8wekyb3d8bbwe!App` This will fallback to launching `WindowsTerminal.exe` if it fails to discover the app user model id to launch. This also fixes a bug in elevate-shim where the first argument of WinMain was lost (e.g. `new-tab`). Curiously, `AppLogic::RunAsUwp()` is never called and `AppLogic::IsUwp()` is always false when running debug builds locally (e.g. WindowsTerminalDev). It's not clear if this is an artifact of development packages or something else. ## Validation Steps Performed Various manual debug/execution scenarios. Verified the fallback path by running the unbundled app by extracting the `CascadiaPackage_0.0.1.0_x64.msix` from the 'drop' build artifact. Fixes #14501 (cherry picked from commit eab1c23) Service-Card-Id: 87690177 Service-Version: 1.16
🎉 Handy links: |
1 similar comment
🎉 Handy links: |
"Release notes" link in bot posts is 404'd. |
This uses
shell:AppsFolder
to launch elevated instances of the app viaShellExecuteEx
andrunas
in elevate-shim.exe. The app to launch isdiscovered via the
GetCurrentApplicationUserModelId
API.e.g.
shell:AppsFolder\WindowsTerminalDev_8wekyb3d8bbwe!App
This will fallback to launching
WindowsTerminal.exe
if it fails todiscover the app user model id to launch.
This also fixes a bug in elevate-shim where the first argument of
WinMain was lost (e.g.
new-tab
).Curiously,
AppLogic::RunAsUwp()
is never called andAppLogic::IsUwp()
is always false when running debug builds locally(e.g. WindowsTerminalDev). It's not clear if this is an artifact of
development packages or something else.
Validation Steps Performed
Various manual debug/execution scenarios.
Verified the fallback path by running the unbundled app by extracting
the
CascadiaPackage_0.0.1.0_x64.msix
from the 'drop' build artifact.Fixes #14501