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.
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
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
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});
user7860670's answer is correct, but I like create_no_window better.
::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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With