Capture output from the system() function

Started by
2 comments, last by v3c7r0n 18 years, 8 months ago
I'm playing with some network stuff right now for a server. its all console right now, and it's also all Win32, so keep that in mind. One of the diagnostics i am working is a tool for pinging the active clients, and in a separate prog just as a test, you can feed it an ip range (which is working) and it will ping it, however what i want to know is if you can somehow capture the output from the ping command so i can display it inside my game. now, since ping runs at the command line i would think there would be a way. ping's output typically looks like this: Pinging <ip address> with 32 bytes of data: <ping time here or time out message> <ping time here or time out message> <ping time here or time out message> <ping time here or time out message> Ping Statistics for <same ip address here>: Packets: Sent = 4, Recieved = (varies based on results), Lost = (again varies) Approximate round trip times in milli-seconds: Minimum = (result), Maximum = (result), Average = (result) All i'm REALLY interested is that last little bit about the ping statistics, because it'd be nice when you're trying to get a game going (LAN or internet) and you can ping everyone from the server. Any help would be apprechiated.
"We are Pentium of Borg. Division is futile, you will be approximated."
Advertisement
The only way you can do it with system() that I can think of is to redirect the shell output to a file and read from that file. A much more elegant way to do that is to spawn a child process connected to the current process with a pipe. I know that in *nix you use popen() but I don't know how to do it on Windows. Maybe something like CreateProcess() or similar can help, but I'm too lazy to look it up myself. [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
On a win32 system, when a program creates a child process, the child process inherits several attributes from the parent process, including the parent's standard handles. You can use this to create a child process with redirected standard input and output via the SetStdHandle() function.

See also: MSDN.
Thanks guys, I figured i'd have to start another process but wasnt really sure. That MSDN link looks like exactly what i need. Maybe one day microsoft will organize their site so one can just say blatently what you want and actually get answers to what you want without getting 2 million specific issue results
"We are Pentium of Borg. Division is futile, you will be approximated."

This topic is closed to new replies.

Advertisement