我想使用 Boost.Process (我使用我使用的源的文档 )来创建一些服务,但是在测试 lib 时发现我什至无法跟踪一个简单的 cmd 应用程序,比如(注意:在同一个文件夹中,我可以完美地启动我的应用程序) .type
type "./config.xml"
我试过的:
我试过同步:
#include <boost/process.hpp>
#include <string>
#include <vector>
#include <iostream>
namespace bp = ::boost::process;
bp::child start_child()
{
std::string exec = "type \"./config.xml\"";
std::vector<std::string> args;
bp::context ctx;
ctx.stdout_behavior = bp::capture_stream();
return bp::launch_shell(exec, args, ctx);
}
int main()
{
bp::child c = start_child();
bp::pistream &is = c.get_stdout();
std::string line;
while (std::getline(is, line))
std::cout << line << std::endl;
std::cin.get();
}
还有异步:
#if defined(__CYGWIN__)
# define _WIN32_WINNT 0x0501
# define __USE_W32_SOCKETS
# undef BOOST_POSIX_API
# define BOOST_WINDOWS_API
#endif
#include <boost/asio.hpp>
#define BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE
#include <boost/process.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <string>
#include <vector>
#include <iostream>
namespace bp = ::boost::process;
namespace ba = ::boost::asio;
ba::io_service io_service;
boost::array<char, 4096> buffer;
#if defined(BOOST_POSIX_API)
ba::posix::stream_descriptor in(io_service);
#elif defined(BOOST_WINDOWS_API)
ba::windows::stream_handle in(io_service);
#else
# error "Unsupported platform."
#endif
bp::child start_child()
{
std::string exec = "type \"./config.xml\"";
bp::context ctx;
ctx.stdout_behavior = bp::capture_stream();
return bp::launch_shell(exec, ctx);
}
void end_read(const boost::system::error_code &ec, std::size_t bytes_transferred);
void begin_read()
{
in.async_read_some(boost::asio::buffer(buffer),
boost::bind(&end_read, ba::placeholders::error, ba::placeholders::bytes_transferred));
}
void end_read(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
if (!ec)
{
std::cout << std::string(buffer.data(), bytes_transferred) << std::flush;
begin_read();
}
}
int main()
{
bp::child c = start_child();
bp::pistream &is = c.get_stdout();
in.assign(is.handle().release());
begin_read();
c.wait();
std::cin.get();
}
我最近看到 anethig 从中出来。就像它根本无法抓取type
流=((而我所有的其他测试都说echo
并且ping
像魅力一样工作)有什么方法可以使 Boost.Processtype
正确地与命令行应用程序一起工作?