diff --git a/src/cascadia/ElevateShim/elevate-shim.cpp b/src/cascadia/ElevateShim/elevate-shim.cpp index 6a78959f4..2a20c33af 100644 --- a/src/cascadia/ElevateShim/elevate-shim.cpp +++ b/src/cascadia/ElevateShim/elevate-shim.cpp @@ -8,14 +8,27 @@ #include #include +// BODGY +// +// If we try to do this in the Terminal itself, then there's a bunch of weird +// things that can go wrong and prevent the elevated Terminal window from +// getting created. Specifically, if the origin Terminal exits right away after +// spawning the elevated WT, then ShellExecute might not successfully complete +// the elevation. What's even more, the Terminal will mysteriously crash +// somewhere in XAML land. +// +// To mitigate this, the Terminal will call into us with the commandline it +// wants elevated. We'll hang around until ShellExecute is finished, so that the +// process can successfully elevate. + #pragma warning(suppress : 26461) // we can't change the signature of wWinMain int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR pCmdLine, int) { + // All of the args passed to us (something like `new-tab -p {guid}`) are in + // pCmdLine + + // Get the path to WindowsTerminal.exe, which should live next to us. std::filesystem::path module{ wil::GetModuleFileNameW(nullptr) }; - - // Cache our name (elevate-shim) - std::wstring ourFilename{ module.filename() }; - // Swap elevate-shim.exe for WindowsTerminal.exe module.replace_filename(L"WindowsTerminal.exe"); @@ -23,9 +36,9 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR pCmdLine, int) SHELLEXECUTEINFOW seInfo{ 0 }; seInfo.cbSize = sizeof(seInfo); seInfo.fMask = SEE_MASK_DEFAULT; - seInfo.lpVerb = L"runas"; - seInfo.lpFile = module.c_str(); - seInfo.lpParameters = pCmdLine; + seInfo.lpVerb = L"runas"; // This asks the shell to elevate the process + seInfo.lpFile = module.c_str(); // This is `...\WindowsTerminal.exe` + seInfo.lpParameters = pCmdLine; // This is `new-tab -p {guid}` seInfo.nShow = SW_SHOWNORMAL; LOG_IF_WIN32_BOOL_FALSE(ShellExecuteExW(&seInfo)); } diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index e4533c6a1..d4d00ca3d 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -550,26 +550,9 @@ namespace winrt::TerminalApp::implementation // However, we need to make sure to close this window in that scenario. // Since there aren't any _tabs_ in this window, we won't ever get a // closed event. So do it manually. - // auto weakThis{ get_weak() }; if (_tabs.Size() == 0) { - // This is MENTAL. If we exit right away after spawning the elevated - // WT, then ShellExecute might not successfully complete the - // elevation. What's even more, the Terminal will mysteriously crash - // somewhere in XAML land. - // - // So I'm introducing a 5s delay here for the Shell to complete the - // execution, and _then_ close the window. - // - // TODO! There's no way this is the right answer, right? - // co_await winrt::resume_background(); - // if (auto page{ weakThis.get() }) - // { - // Sleep(5000); - // co_await winrt::resume_foreground(page->Dispatcher(), CoreDispatcherPriority::Normal); - // page->_LastTabClosedHandlers(*page, nullptr); _LastTabClosedHandlers(*this, nullptr); - // } } else { @@ -3576,11 +3559,12 @@ namespace winrt::TerminalApp::implementation // Hop to the BG thread co_await winrt::resume_background(); - // This is supremely dumb. We're going to construct the commandline we - // want, then toss it to a helper process called `elevate-shim.exe` that - // happens to live next to us. elevate-shim.exe will be the one to call - // ShellExecute with the args that we want (to elevate the given - // profile). + // BODGY + // + // We're going to construct the commandline we want, then toss it to a + // helper process called `elevate-shim.exe` that happens to live next to + // us. elevate-shim.exe will be the one to call ShellExecute with the + // args that we want (to elevate the given profile). // // We can't be the one to call ShellExecute ourselves. ShellExecute // requires that the calling process stays alive until the child is @@ -3611,6 +3595,10 @@ namespace winrt::TerminalApp::implementation nullptr, &si, &pi)); + + // TODO: GH#8592 - It may be useful to pop a Toast here in the original + // Terminal window informing the user that the tab was opened in a new + // window. } // Method Description: