[英]Boost::process hide console on windows
Recently boost 1.64 released, including boost::process.最近发布了 boost 1.64,包括 boost::process。 This provides an easy interface for starting processes.
这为启动进程提供了一个简单的界面。 Previously I used the stand-alone version of the boost::process library (see here ).
以前我使用的是 boost::process 库的独立版本(请参阅此处)。 This worked well.
这工作得很好。 I would like to change to the new edition so I can drop the stand-alone dependency.
我想更改为新版本,以便可以删除独立依赖项。
The API is a bit different but everything works fine, except for on thing. API 有点不同,但一切正常,除了 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.
在旧版本中,我能够传递特定于 Windows 的上下文对象,该对象允许我隐藏进程打开的任何控制台窗口。
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
constructor accepts a list of types that will be later converted using fancy ::boost::fusion
methods into chain of calls performing actual initializations. child
构造函数接受一个类型列表,稍后将使用花哨的::boost::fusion
方法将这些类型转换为执行实际初始化的调用链。 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
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.有条件地隐藏窗口并不是那么简单,因为
windows::hide
和windows::show
是不同的类型,不能通过相同的函数参数传递。 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});
user7860670's answer is correct, but I like create_no_window better. user7860670 的回答是正确的,但我更喜欢 create_no_window。
::boost::process::child ch1("myApp.exe", ::boost::process::windows::create_no_window);
If you don't need the window throughout, you do not need to create it at all.如果您不需要整个窗口,则根本不需要创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.