Hier ein bisschen Code, um ein externes Programm zu starten und Eingabe bzw Ausgabe umzuleiten. Das ganze funzt derzeit nur unter Windows, falls jemand sowas unter Windows braucht, Bescheid sagen.
Das ganze funktioniert derzeit nur mit Text-Ein- und Ausgabe, da für die Daten Strings verwendet werden, auch ist die Implementation teilweise etwas umständlich. Es wird sowohl synchrones als auch asynchrones Aufrufen unterstützt.
TODO:
- Windows-Support
- Binäre Ausgabe/Eingabe
- Callbacks, wenn Daten ankommen
- Ein bisschen mehr Dokumentation
Klassen-Deklaration:
Code:
class Process
{
public:
Process(std::string filename = "");
~Process();
bool call(void);
bool callSync(int *retval = 0);
void setFilename(std::string filename);
std::string getFilename(void);
void setArguments(std::vector<std::string> args);
std::vector<std::string> getArguments(void);
void addArgument(std::string arg);
void clearArguments(void);
bool isRunning(void);
void kill(void);
void join(int *retval);
bool stdin(std::string input);
bool stdout(std::string &output);
bool stderr(std::string &output);
int getReturnValue(void);
void callThread(void);
};
EDIT:
Der Code unterliegt der WTFPL. Falls das gegen die Regeln für Codesnippets verstößt, bitte melden.
Die enthalten aber eine Formulierung, die in Deutschland so nicht geht (Es gibt kein Public Domain!).
Zitat:
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar
14 rue de Plaisance, 75014 Paris, France
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
*
http://sam.zoy.org/wtfpl/COPYING for more details.