I have this
static std::string exec(char* cmd) {
FILE* pipe = _popen(cmd, "r");
if (!pipe) return "ERROR -22";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
_pclose(pipe);
return result;
}
But the problem i have is that i want to hide the program that lunches how can I do that? thx
As Hans Passant mentioned in the comments, you have to use
CreateProcessto spawn the child process instead of_popen, since_popendoes not give you any control over the window creation process.To hide the console window, use the
CREATE_NO_WINDOWprocess creation flag for thedwCreationFlagsparameter.In order to capture the output of the process, you need to create a pipe for its standard output stream with
CreatePipe. Assign that pipe handle to thehStdOutputmember of theSTARTUPINFOstructure you pass in, and make sure to set theSTARTF_USESTDHANDLESflag in the startup info’sdwFlagsso it knows that that member is valid. Then, to read data, you just useReadFileon the pipe handle.Hans also provided a link to a good example of creating pipes with a child process here, although that example is doing more than you need to do—it’s creating pipes for all three streams (stdin, stdout, and stderr), whereas you only need to capture stdout.