如何使用Boost.Process 0.5运行命令行/终端工具?

时间:2012-10-07 00:36:24

标签: c++ boost process

我发现有一个新的Boost.Process 0.5,但我无法看到如何跨Windows Linux和Mac pingecho执行。

我在Windows上用leaast工作很简单:

#include <string>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/error_code.hpp>

namespace bp = boost::process;
namespace bpi = boost::process::initializers;
namespace bio = boost::iostreams;
int main()
{
    bp::pipe p = bp::create_pipe();
    {
        bio::file_descriptor_sink sink(p.sink, bio::close_handle);
        boost::filesystem::path p("C:/Windows/System32/cmd.exe");
        boost::system::error_code ec;
        bp::execute(
            bpi::run_exe(p),
            bpi::set_cmd_line(L"cmd /c echo --echo-stderr hello"),
            bpi::bind_stdout(sink),
            bpi::set_on_error(ec)
            );
    }

    bio::file_descriptor_source source(p.source, bio::close_handle);
    bio::stream<bio::file_descriptor_source> is(source);

    std::string s;
    is >> s;
    std::cout << s << std::endl;
    std::cin.get();
    return 0;
}

在Windows上这可以正常工作,但是如何让它跨平台在Mac和Linux上也能正常工作? (我是愚蠢的,不知道如何编写一个适用于任何Unix终端的路径(或者至少用于Linux Bash和mac默认路径))那么如何在Windows和Unix上运行命令行/终端工具与Boost.Process 0.5喜欢操作系统(最好不要每次只写路径到终端,而只是写echoping及其参数等应用程序?

...在prevoius版本中找到相关代码:

    std::string exe; 
    std::vector<std::string> args; 

#if defined(BOOST_POSIX_API) 
    exe = "/bin/sh"; 
    args.push_back("sh"); 
    args.push_back("-c"); 
    args.push_back(command); 
#elif defined(BOOST_WINDOWS_API) 
    char sysdir[MAX_PATH]; 
    UINT size = ::GetSystemDirectoryA(sysdir, sizeof(sysdir)); 
    if (!size) 
        boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::launch_shell: GetWindowsDirectory failed")); 
    BOOST_ASSERT(size < MAX_PATH); 

    exe = std::string(sysdir) + (sysdir[size - 1] != '\\' ? "\\cmd.exe" : "cmd.exe"); 
    args.push_back("cmd"); 
    args.push_back("/c"); 
    args.push_back(command); 
#endif 

1 个答案:

答案 0 :(得分:1)

在boost.process 0.5下引入了shell_path() API,因此以下内容可能会引起您的兴趣

#if defined(BOOST_POSIX_API) 
    #define SHELL_COMMAND_PREFIX "-c"
#elif defined(BOOST_WINDOWS_API) 
    #define SHELL_COMMAND_PREFIX "/c"
#endif


filesystem::path shellPath = process::shell_path();
std::string cl = shell_path().string() + " " SHELL_COMMAND_PREFIX " ";
cl += "ping 127.0.0.1";
execute(  
        set_cmd_line(cl),
        throw_on_error()
);

如果你真的想隐藏#ifdef,我会继续编辑boost源以返回相关的命令前缀(添加新的API),开源之后不是吗? :)。 您可以在boost / process / windows / shell_path.hpp和boost / process / posix / shell_path.hpp中找到要编辑的相关源代码