Boost process在新窗口打开进程 - 小众知识

Boost process在新窗口打开进程

2022-08-05 08:16:10 苏内容
  标签: Boost/process
阅读:2526

Boost process open process in new window

'm trying to design a program that uses workers processes - which are just a different program written in C++.

I start a worker process like so:

auto worker = boost::process::child("./worker.exe");
worker->detach();

The issue is, is that the worker processes are outputting information to the same command line window that they are spawned from. This is cluttering the output of the program. Ideally I want each process to run in its own window.

Is this possible using boost::process? I've only found information about hiding the window.

I'm using Windows and Visual Studio 2019.

Thanks


As you can see in Boost::process hide console on windows, you can create new mode for creating process.

CreateProcessA function system call, Show yourself to how to create new process with new console, by helping creation flags: CREATE_NEW_CONSOLE (thanks to Using multiple console windows for output)

you can write code like below

struct new_window:  

::boost::process::detail::handler_base

{    

// this function will be invoked at child process constructor before spawning process

   template <class WindowsExecutor>    

void on_setup(WindowsExecutor &e) const

   {        e.creation_flags = ::boost::detail::winapi::CREATE_NEW_CONSOLE_;    } };

and for using this, you can just write something like below

::boost::process::child ch("./worker.exe", new_window);



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


主要是在创建进程时,使用

CREATE_NEW_CONSOLE

这个标签


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1