2011-09-15 4 views
1

だから、ブースト:: process(私はboost sandbox svn最新から取ることを)私たちは打ち上げ1つのアプリのようなものを行うと、ファイルWIA、このようなコードに出力をリダイレクトすることができますと:Boost :: process - どのようにして1つのプロセスだけを実行させるのですか?

#include <string> 
#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <iterator> 
#include <boost/asio.hpp> 
#include <boost/process.hpp> 
#include <boost/filesystem.hpp> 
#include <boost/foreach.hpp> 

std::vector<std::string> split(const std::string& s, const std::string& delim, const bool keep_empty = false) { 
    std::vector<std::string> result; 
    if (delim.empty()) { 
     result.push_back(s); 
     return result; 
    } 
    std::string::const_iterator substart = s.begin(), subend; 
    while (true) { 
     subend = search(substart, s.end(), delim.begin(), delim.end()); 
     std::string temp(substart, subend); 
     if (keep_empty || !temp.empty()) { 
      result.push_back(temp); 
     } 
     if (subend == s.end()) { 
      break; 
     } 
     substart = subend + delim.size(); 
    } 
    return result; 
} 

boost::process::child start_child(boost::filesystem::path path_to_exec, std::string arguments) 
{ 

    std::string exec = path_to_exec.string(); 
    boost::process::context ctx; 
    ctx.environment = boost::process::self::get_environment(); 
    ctx.stdout_behavior = boost::process::capture_stream(); 

    #if defined(BOOST_POSIX_API) 
     return boost::process::launch(exec, split(arguments, " "), ctx); 
    #elif defined(BOOST_WINDOWS_API) 
     return boost::process::launch_shell(exec + " " + arguments, ctx); 
    #else 
    # error "Unsupported platform." 
    #endif 
} 


int main() 
{ 
    boost::filesystem::path exec = boost::filesystem::current_path(); 
    exec /= "CloudClient/CloudClient.exe"; 
    boost::process::child c = start_child(exec, "--server=127.0.0.1:4773/ --username=robota --robot > file.a"); 
    boost::process::pistream &is = c.get_stdout(); 
    std::string line; 
    while (std::getline(is, line)) 
     std::cout << line << std::endl; 

    boost::process::status s = c.wait(); 
    std::cin.get(); 
} 

しかし、私はそれようにそれを制限したいです1つのプロセス - aninアプリケーションのみを起動し、そのようなパイプを作成することはできません。 boost::process::child start_child(boost::filesystem::path path_to_exec, std::string arguments)の機能を安全にしたり、少なくとも私が欲しいことを心配することなく安全にする方法はありますか?


ところで:Windows上で私は、私は多分、次のことを=(

+1

'Boost.Process' は、実際に一部ではありません。 –

+0

@ K-ballo:[boost/sandbox/proess](http://svn.boost.org)へのリンクを追加しました。/svn/boost/sandbox/process /)(私はそこから最新のsvnリビジョンを取る) – Rella

+0

@Kabumbus:That **最新のBoost.Processコードではありません**元の開発者の最新コードですが、昨年誰かによって引き継がれ、他の場所でコードをホストしました。 Boost MLアーカイブを掘り下げてみると、見つけにくいはずはありません。 – ildjarn

答えて

0

を起動しようとするアプリを破砕してreturn boost::process::launch(exec, split(arguments, " "), ctx);を使用することはできません

bp::context ctx; 
ctx.stdout_behavior = bp::silence_stream(); 
bp::launch(exec, args, ctx); 
関連する問題