Invalid shell commands and _popen()

1
2
3
4
5
6
FILE* pipe = _popen(command.c_str(), "r");

while(fgets(data, 1024, pipe))
	g_Client.Send(data);

_pclose(pipe);


So above executes shell commands, reads stream from pipe and sends the data to my server, ex:

I execute time and the program successfully sends

The current time is: 19:50:42.24


If I execute an invalid command or one with a syntax error like cd %userprfile%, however, the data:

The system cannot find the path specified.

isn't sent to the server via the g_Client.Send() function like it would with a valid command; instead it's printed to the console window.

How do I capture the output from an invalid command and why isn't fgets() storing it in the data variable like output from valid commands?
> why isn't fgets() storing it in the data variable like output from valid commands?

Error messages are sent to stderr, normal output is sent to stdout.


> How do I capture the output from an invalid command

Redirect stderr output to stdout with 2>&1

For instance, with _popen( command.c_str() + " 2>&1", "r");

See: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx
Last edited on
Much obliged man. Curious operand along with that first parameter, page you linked is gold. Failed to find a solution after a good bit of searching, you saved me precious mental resources not having to waste more time extensively googling :P HQ response and website.

Tyvm
Last edited on
Topic archived. No new replies allowed.