2012-10-07 71 views
3

我發現有一個新的Boost.Process 0.5,但我不知道如何執行Windows Linux和Mac pingecho如何使用Boost.Process 0.5運行命令行/終端utils?

我得到了它在leaast在Windows上工作簡單:

#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運行命令行/終端utils像操作系統(最好不要每次寫入終端的路徑,而只是像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

在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找到相關的源代碼。