Search code examples
c++boostwindow

Is there a way to create a new console window for process spawned with Boost?


I am writing an application which must spawn and detach a new process which has a separate console window which can be used for user input and output. Ideally the solution should be cross platform and a solution which uses Boost is even better (since Boost is already used extensively within this project).

I have already tried boost::process::spawn but that only launches the new process in the same console window as the the main process. CreateProcess (in the Win-API) with the CREATE_NEW_CONSOLE flag works as a temporary fix, but a cross platform solution is desired.


Solution

  • Yes, you can do it through handlers https://www.boost.org/doc/libs/1_64_0/doc/html/boost_process/extend.html

    Make a handler that adds CREATE_NEW_CONSOLE to creation_flags:

    struct new_console : bp::extend::handler {
        template <typename Sequence>
        void on_setup(bp::extend::windows_executor<char, Sequence> & ex) {
            ex.creation_flags |= CREATE_NEW_CONSOLE;
        }
    }
    

    Then you can use it as an argument for child creation:

    std::error_code ec;
    boost::process::child child(exe, args, new_console(), ec);