1

So with boost::process (that I take from latest boost sandbox svn) we can do stuff like launch one app and redirect output into a file wia such code:

#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();
} 

But I want to limit that so that it would launch only one process - anin application and would not be capable to create such pipes. Is it any how possible to make my boost::process::child start_child(boost::filesystem::path path_to_exec, std::string arguments) function safe or at least safer in concern to what I want?


BTW: on windows I can not use return boost::process::launch(exec, split(arguments, " "), ctx); with out crushing app that I try to start =(

Rella
  • 65,003
  • 109
  • 363
  • 636
  • 1
    'Boost.Process' is not actually part of boost, and there are several different versions laying around. You may want to point out which one you are using. – K-ballo Sep 15 '11 at 21:52
  • @K-ballo: Added link to [boost/sandbox/proess](http://svn.boost.org/svn/boost/sandbox/process/) (I take latest svn revision from there) – Rella Sep 15 '11 at 22:02
  • @Kabumbus : That is definitely **not** the latest Boost.Process code -- that's the latest code by the original developer, but it was taken over last year by someone else and they hosted their code elsewhere. Dig around the Boost ML archives, it shouldn't be hard to find. – ildjarn Sep 16 '11 at 00:47
  • 1
    @Kabumbus : Ah, found it; latest Boost.Process code is [here](https://github.com/JeffFlinn/boost-process). – ildjarn Sep 16 '11 at 01:10
  • I'm not sure what you're actually asking. You want to limit `start_child()` to only start one process, but given the code you execute in `main()` this is already happening. What is the actual result you seek? –  Sep 18 '11 at 09:31
  • If you want to ensure there is only ever one CloudClient running you would need to either modify CloudClient to check there is not a version of it running already, maybe using a named mutex. Or if you cannot modify CloudClient, you would need to go platform specific: http://stackoverflow.com/questions/1591342/how-to-determine-if-a-windows-process-is-running – ashleysmithgpu Sep 22 '11 at 09:08
  • Your question isn't very clear. I'm familiar with boost, but I have no idea what you're trying to accomplish... – Mahmoud Al-Qudsi Sep 25 '11 at 02:28

1 Answers1

0

Maybe the following.

bp::context ctx;
ctx.stdout_behavior = bp::silence_stream();
bp::launch(exec, args, ctx);
Phil
  • 46,436
  • 33
  • 110
  • 175