7

I have written a simple program that pings three sites and then reacts to whether they are reachable or not.

My question is: can I suppress system("ping ")'s output? I have written my code in C++ as I know that language the best. Currently the code opens the ping.exe running the system command. If I can prevent the output from showing up while it still pings that would be ideal.

I am eventually going to turn this program in a windows service that is why I would like to suppress both the command line console window as well as suppress the ping output. Thanks.

4

6 に答える 6

20

Try doing system("ping host > nul") (nul is windows equivalent of UNIX /dev/null).

于 2010-10-31T06:52:19.690 に答える
6

Generally, if you're going to call another program but don't want it to act like std::system, you're going to need a platform-specific function like fork()/exec() on UNIX or CreateProcess() on Windows. These functions give you control over how the other program runs, for instance, that it not show output or not create a console window, etc.

于 2010-10-31T06:59:44.440 に答える
3

You can use system command like below to suppress the output of ping command.

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

Above command pings IP address 100.100.100.100 and directs the output to a file called response.dat. In response.dat you can see the response of ping command.

于 2010-10-31T06:51:54.143 に答える
2

この方法を使用することもできます。これにより、出力がファイルに返され、コンソールウィンドウが表示されず、メインアプリケーションがフリーズします。これは非常に便利です。最初に、を使用してWindowsヘッダーを含める必要があります。

#include <Windows.h>

次に、pingコマンドを送信し、出力を次のようなファイルに書き込みます。

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

これにより、pingコマンドがgoogle.comに送信され、現在実行中のプログラムのディレクトリにあるファイル「file.dat」に出力が書き込まれます。したがって、file.datを任意のファイルまたはファイルパスに変更できます。もちろん、pingコマンドを変更することもできます。>文字は、コマンドの出力をその背後のファイルパスに書き込む必要があることを意味します。pingコマンドの実行中にコンソールウィンドウを表示してアプリケーションをフリーズする場合は、WindExec()コードの代わりに次のコード行を使用する必要があります。

system("ping google.com > file.dat");
于 2012-03-13T14:19:13.200 に答える
2

Do system( "ping site.com >nul 2>nul" ); and check the value the shell returns. if the ping succeeds, the shell will return 0, else it will return 1. I would be more detailed, but Vis Studio is reinstalling itself. :)

There's also a way to hide the console window using the Win API to exec the command, but... I do not remember the details.

Edit: I'm still waiting for the MSVS install process, so... :) Use CreateProcess with the DETACHED_PROCESS flag for the dwCreationFlags parameter to hide the console window.

After you call create process, you'll have to use WaitForSingleObject on the process handle to wait for the ping to complete. The last parameter to CreateProcess should have a pointer to process information that contains the process handle. (Assuming CreateProcess was successful) You have to wait for the command to complete. Once it's complete, you can use the process handle to get the return value, though I'm too time contstrained to tell you how to do that at this point.

于 2010-10-31T06:57:48.573 に答える
2

When you get over to Windows and call CreateProcess(), be sure to set:

    lpStartupInfo->wShowWindow = SW_HIDE;

This will ensure that any windows created by the new process are hidden.

Using the DETACHED_PROCESS flag will prevent the new process from inheriting your application's console, but that does not prevent the new process from creating a new console. Not sure what ping would do, but best to remove all doubt by using SW_HIDE.

于 2010-10-31T17:09:28.487 に答える