有没有办法为使用 Boost 生成的进程创建一个新的控制台 window?

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

我正在编写一个应用程序,它必须生成并分离一个新进程,该进程有一个单独的控制台 window,可用于用户输入和输出。理想情况下,解决方案应该是跨平台的,使用 Boost 的解决方案更好(因为 Boost 已经在这个项目中广泛使用)。

我已经尝试过 boost::process::spawn 但那只会在与主进程相同的控制台 window 中启动新进程。 CreateProcess(在 Win-API 中)带有 CREATE_NEW_CONSOLE 标志作为临时修复,但需要跨平台解决方案。

是的,您可以通过处理程序完成 https://www.boost.org/doc/libs/1_64_0/doc/html/boost_process/extend.html

创建一个处理程序,将 CREATE_NEW_CONSOLE 添加到 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;
    }
}

然后你可以用它作为子创建的参数:

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