Boost::process hide console on windows

前端 未结 1 1733
感情败类
感情败类 2021-01-19 15:43

Recently boost 1.64 released, including boost::process. This provides an easy interface for starting processes. Previously I used the stand-alone version of the boost::proce

相关标签:
1条回答
  • 2021-01-19 16:26

    child constructor accepts a list of types that will be later converted using fancy ::boost::fusion methods into chain of calls performing actual initializations. So you can just push arguments of supported kind in any order:

    #include <boost/process.hpp>
    #include <boost/process/windows.hpp> // for windows::hide that can only be used on Windows
    
    ...
    
    ::boost::process::environment env = ::boost::this_process::environment();
    ::boost::process::child ch1("cmd", env, ::boost::process::windows::hide); // ok
    ::boost::process::child ch2(::boost::filesystem::path("C:\\Windows\\System32\\cmd.exe"), ::boost::process::windows::hide, env); // fine too
    

    Hiding window conditionally is not that straightforward though because windows::hide and windows::show are of different types and can not be passed at the same function parameter. In this case it is required to write custom setup handler:

    struct show_window
    :   ::boost::process::detail::handler_base
    {
        private: ::boost::detail::winapi::WORD_ const m_flag;
    
        public: explicit
        show_window(bool const show) noexcept
        :   m_flag{show ? ::boost::detail::winapi::SW_SHOWNORMAL_ : ::boost::detail::winapi::SW_HIDE_}
        {}
    
        // this function will be invoked at child process constructor before spawning process
        template <class WindowsExecutor>
        void on_setup(WindowsExecutor &e) const
        {
            // we have a chance to adjust startup info
            e.startup_info.dwFlags |= ::boost::detail::winapi::STARTF_USESHOWWINDOW_;
            e.startup_info.wShowWindow |= m_flag;
        }
    };
    
    auto const need_to_show{false};
    auto env{::boost::this_process::environment()};
    ::boost::process::child ch("cmd", env, show_window{need_to_show});
    
    0 讨论(0)
提交回复
热议问题