Suppress system ("ping") output in C ++

I wrote a simple program that pings three sites, and then responds to whether they are reachable or not.

My question is: can I suppress the system output ("ping")? I wrote my C ++ code since I know that the language is better. Currently, the code opens ping.exe by running the system command. If I can stop the output from being output while it still works, that would be perfect.

I will eventually turn this program into a Windows service, so I would like to suppress the command line console window as well as suppress ping output. Thank.

+5
source share
6 answers

system("ping host > nul") (nul Windows UNIX /dev/null).

+16

, , , std::system, , , fork()/exec() UNIX CreateProcess() Windows. , , , ..

+6

, , ping.

system("ping 100.100.100.100 > response.dat");

IP- 100.100.100.100 response.dat. response.dat ping.

+3

( "ping site.com > nul 2 > nul" ); , . ping , 0, 1. , Vis Studio .:)

Win API , ... .

: MSVS, ...:) CreateProcess DETACHED_PROCESS dwCreationFlags, .

WaitForSingleObject , ping. CreateProcess , . (, CreateProcess ). . , , , .

+2

Windows CreateProcess(), :

    lpStartupInfo->wShowWindow = SW_HIDE;

, , , .

DETACHED_PROCESS , . , ping, SW_HIDE.

+2

, , . Windows,

#include <Windows.h>

ping , :

WinExec("ping google.com > file.dat", SW_HIDE); 

This will send the ping command to google.com and write the output to the file 'file.dat' in the directory of your currently running program. This way you can change file.dat to any file or file path you want, and of course you can change the ping command. The symbol> means that the output of the command must be written to the file path behind it. If you want to show the console window and freeze the application during the ping command, you need to use the following line of code instead of WindExec () code;

system("ping google.com > file.dat");
+2
source

All Articles