Windows上的Boost :: process隐藏控制台 [英] Boost::process hide console on windows
问题描述
最近发布了boost 1.64,包括boost :: process.这为启动过程提供了简单的界面.以前,我使用了boost :: process库的独立版本(请参见此处).这很好.我想更改为新版本,以便删除独立依赖项.
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::process library (see here). This worked well. I would like to change to the new edition so I can drop the stand-alone dependency.
API稍有不同,但是除了on之外,其他一切都可以正常工作.在旧版本中,我能够传递一个Windows特定的上下文对象,该对象允许我隐藏该进程打开的任何控制台窗口.
The API is a bit different but everything works fine, except for on thing. In the old version I was able to pass a windows-specific context object which allowed me the hide any console windows openened by the process.
boost::process::win32_context ctx;
ctx.environment = boost::process::self::get_environment();
STARTUPINFOA stup;
ZeroMemory(&stup, sizeof(stup));
stup.cb = sizeof(stup);
stup.dwFlags = STARTF_USESHOWWINDOW;
stup.wShowWindow = SW_HIDE;
ctx.startupinfo = &stup;
std::vector<std::string> args;
boost::process:child process = boost::process::win32_launch("myprogram", args, ctx);
使用新版本看起来像这样:
Using the new version it looks like this:
boost::process::environment env = boost::this_process::environment();
boost::process:child process(boost::filesystem::path("myprogram"), env);
除隐藏控制台窗口外,其他所有操作均正常.有可能做到这一点吗?
Everything works fine except for hiding the console window. Is it possible to achieve this?
推荐答案
child
构造函数接受类型列表,这些类型以后将使用 :: boost :: fusion
进行转换>将方法分成执行实际初始化的调用链.因此,您可以按任意顺序推送受支持类型的参数:
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
有条件地隐藏窗口并不是那么简单,因为 windows :: hide
和 windows :: show
具有不同的类型,并且不能在相同的函数参数处传递.在这种情况下,需要编写自定义设置处理程序:
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});
这篇关于Windows上的Boost :: process隐藏控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!