windows 在 C++ 中抑制系统(“ping”)输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4062106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:30:34  来源:igfitidea点击:

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

c++windowspingsuppress

提问by Samuel

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

我编写了一个简单的程序,可以 ping 三个站点,然后对它们是否可达做出反应。

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.

我的问题是:我可以抑制系统(“ping”)的输出吗?我已经用 C++ 编写了我的代码,因为我最了解这种语言。目前,代码会打开运行系统命令的 ping.exe。如果我可以阻止输出显示而它仍然可以 ping 那将是理想的。

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.

我最终要在 Windows 服务中打开这个程序,这就是为什么我想抑制命令行控制台窗口以及抑制 ping 输出。谢谢。

回答by zvrba

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

尝试做system("ping host > nul")( nulWindows 相当于 UNIX /dev/null)。

回答by Max Lybbert

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.

一般来说,如果你要调用另外一个程序,但不希望它像std::system,你会需要像一个特定于平台的功能fork()/ exec()UNIX或CreateProcess()Windows上。这些函数使您可以控制其他程序的运行方式,例如,它不显示输出或不创建控制台窗口等。

回答by bjskishore123

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

您可以使用如下系统命令来抑制 ping 命令的输出。

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.

以上命令 ping IP 地址 100.100.100.100 并将输出定向到名为 response.dat 的文件。在 response.dat 中可以看到 ping 命令的响应。

回答by JimR

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. :)

do system("ping site.com >nul 2>nul"); 并检查 shell 返回的值。如果 ping 成功,shell 将返回 0,否则将返回 1。我会更详细,但 Vis Studio 正在重新安装自身。:)

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.

还有一种方法可以使用 Win API 来隐藏控制台窗口来执行命令,但是......我不记得细节了。

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.

编辑:我仍在等待 MSVS 安装过程,所以... :) 将 CreateProcess 与 DETACHED_PROCESS 标志用于 dwCreationFlags 参数以隐藏控制台窗口。

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.

调用 create process 后,您必须在进程句柄上使用 WaitForSingleObject 以等待 ping 完成。CreateProcess 的最后一个参数应该有一个指向包含进程句柄的进程信息的指针。(假设 CreateProcess 成功)您必须等待命令完成。完成后,您可以使用进程句柄来获取返回值,尽管我现在时间有限,无法告诉您如何执行此操作。

回答by CoreTech

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

当您转到 Windows 并调用CreateProcess() 时,请务必设置:

    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.

使用 DETACHED_PROCESS 标志将阻止新进程继承应用程序的控制台,但这不会阻止新进程创建新控制台。不确定 ping 会做什么,但最好使用 SW_HIDE 消除所有疑问。

回答by Tim Visée

You could also use this way, this will return the output in a file and doesn't show up a console windows and freezes the main application which is really usefull. At first you need to include the Windows header using;

您也可以使用这种方式,这将在文件中返回输出并且不显示控制台窗口并冻结真正有用的主应用程序。首先,您需要使用包含 Windows 标头;

#include <Windows.h>

then send a ping command and write the output into a file like this;

然后发送一个 ping 命令并将输出写入这样的文件;

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

This will send a ping command to google.com and writes the output to the file 'file.dat' in the directory of your current running program. So you could change file.dat to any file or filepath you want and of course you could change the ping command. The > character means that the output of the command needs to be wrote in the file path behind it. If you want to show the console window and freeze the application while running the ping command you need to use the following line of code instead of the WindExec() code;

这将向 google.com 发送 ping 命令,并将输出写入当前运行程序目录中的文件“file.dat”。因此,您可以将 file.dat 更改为您想要的任何文件或文件路径,当然您也可以更改 ping 命令。> 字符表示命令的输出需要写在它后面的文件路径中。如果要在运行 ping 命令时显示控制台窗口并冻结应用程序,则需要使用以下代码行而不是 WindExec() 代码;

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