Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/WindowEmperor.cpp (C++)

Code before:
    }
}

bool WindowEmperor::HandleCommandlineArgs()
{
    std::vector<winrt::hstring> args;
    _buildArgsFromCommandline(args);
    const auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };

    {
        // ALWAYS change the _real_ CWD of the Terminal to system32, so that we
        // don't lock the directory we were spawned in.
        std::wstring system32{};
        if (SUCCEEDED_LOG(wil::GetSystemDirectoryW<std::wstring>(system32)))
        {
            LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectoryW(system32.c_str()));
        }
    }

    // Get the requested initial state of the window from our startup info. For
    // something like `start /min`, this will set the wShowWindow member to
    // SW_SHOWMINIMIZED. We'll need to make sure is bubbled all the way through,
    // so we can open a new window with the same state.
    STARTUPINFOW si;
    GetStartupInfoW(&si);
    const uint32_t showWindow = WI_IsFlagSet(si.dwFlags, STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOW;

    const auto currentEnv{ til::env::from_current_environment() };

    Remoting::CommandlineArgs eventArgs{ { args }, { cwd }, showWindow, winrt::hstring{ currentEnv.to_string() } };

    const auto isolatedMode{ _app.Logic().IsolatedMode() };

    const auto result = _manager.ProposeCommandline(eventArgs, isolatedMode);

    const bool makeWindow = result.ShouldCreateWindow();
    if (makeWindow)
    {
        _createNewWindowThread(Remoting::WindowRequestedArgs{ result, eventArgs });

        _becomeMonarch();
    }
    else
    {
        const auto res = _app.Logic().GetParseCommandlineMessage(eventArgs.Commandline());
        if (!res.Message.empty())
        {
            AppHost::s_DisplayMessageBox(res);
            std::quick_exit(res.ExitCode);
        }
    }

Review comment (suggestion):
FYI There's one slight difference between this implementation and the way `nCmdShow` is generated. `__scrt_get_show_window_mode`'s default value is `SW_SHOWDEFAULT`. I believe in our case that difference doesn't really matter though.

Code after:
{
    std::vector<winrt::hstring> args;
    _buildArgsFromCommandline(args);
    const auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };

    {
        // ALWAYS change the _real_ CWD of the Terminal to system32, so that we
        // don't lock the directory we were spawned in.
        std::wstring system32{};
        if (SUCCEEDED_LOG(wil::GetSystemDirectoryW<std::wstring>(system32)))
        {
            LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectoryW(system32.c_str()));
        }
    }

    const Remoting::CommandlineArgs eventArgs{ args, cwd, gsl::narrow_cast<uint32_t>(nCmdShow), GetEnvironmentStringsW() };
    const auto isolatedMode{ _app.Logic().IsolatedMode() };
    const auto result = _manager.ProposeCommandline(eventArgs, isolatedMode);

    const bool makeWindow = result.ShouldCreateWindow();
    if (makeWindow)
    {
        _createNewWindowThread(Remoting::WindowRequestedArgs{ result, eventArgs });

        _becomeMonarch();
    }
    else
    {
        const auto res = _app.Logic().GetParseCommandlineMessage(eventArgs.Commandline());
        if (!res.Message.empty())
        {
            AppHost::s_DisplayMessageBox(res);
            std::quick_exit(res.ExitCode);
        }
    }

    return makeWindow;
}

void WindowEmperor::WaitForWindows()
{
    MSG message{};
    while (GetMessageW(&message, nullptr, 0, 0))
    {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
}

void WindowEmperor::_createNewWindowThread(const Remoting::WindowRequestedArgs& args)
{