有没有办法为 Boost 产生的进程创建一个新的控制台窗口? [英] Is there a way to create a new console window for process spawned with Boost?

查看:45
本文介绍了有没有办法为 Boost 产生的进程创建一个新的控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它必须生成和分离一个新进程,该进程有一个单独的控制台窗口,可用于用户输入和输出.理想情况下,解决方案应该是跨平台的,使用 Boost 的解决方案会更好(因为 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).

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

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.

推荐答案

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

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

制作一个将 CREATE_NEW_CONSOLE 添加到 creation_flags 的处理程序:

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);

这篇关于有没有办法为 Boost 产生的进程创建一个新的控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆