Reputation: 79
I've spent the better part of a week searching for an answer to this (I thought) simple question, and while I've found many others with it, I have not yet found a clear answer, or even anything I could get to work.
My question is this: How can I invoke a child process, and read silently from its stdout pipe into a string, array, or something in the parent process without spawning a floating console window?
So far I have this code borrowed and tweaked slightly:
wls EXEC(string comm) {
wls _internal;
FILE* pipe = popen(comm.c_str(), "r");
if (!pipe)
return _internal;
char buffer[8192];
while (!feof(pipe))
if (fgets(buffer, 8192, pipe) != NULL)
_internal.push_back(ATS(buffer).substr(0, ATS(buffer).size() - 1));
pclose(pipe);
return _internal;
}
Important background info:
wls
is the result of typedef vector<string> wls;
ATS
is a template function I use to pass all sorts of things in and get back a string
Now, this function works perfectly, if we're talking function vs. form. It runs the child process, and I get back an array of strings - one for each line of the child process's output. However, every time it runs, it makes a command prompt window open. I understand that there is no way to avoid this with popen, so I have turned to CreateProcess(). I have not however managed to create an equivilant function to the one above using CreateProcess, and this is what I would like to do.
Could anyone possibly lend a hand? It would be much appreciated, and you would be creating the definitive guide for doing this anywhere on the internet if you are successful :)
Upvotes: 1
Views: 1961
Reputation: 79
Sorry, I guess I should have posted an answer like this instead. Well here it is:
By adding just 2 extra lines of code, I've been able to solve my problem while continuing to use the function (with popen) I already had. If you require absolutely no popups, this won't be good enough for you, but if you can deal with just 1 incredibly brief popup, and nothing ever again after that, this if probably your best answer since it's much shorter than any of the "solutions" I've seen that use CreateProcess (and unlike all of them this actually worked for me first time and very easily).
Add the following 2 lines to the top of the main function of your graphical program:
AllocConsole();
ShowWindow(GetConsoleWindow(), SW_HIDE);
The first one creates and associates a console window with your program. The next line immediately hides it. This does create a popup (an extremely brief one), but since there is now a console window for your application, any and all calls to popen are able to exist in it, rather than creating their own window each and every time.
Upvotes: 1