如何在c++中执行shell程序并获取其stout静默不弹出cmd窗口
例如,我使用 create process win32 函数来启动 Windows shell 应用程序 Ipconfig 并获取其输出,但没有弹出 cmd 窗口。
我还尝试使用 POCO 库及其进程类,但每次都会弹出 cmd。
有谁知道如何使用 POCO 库来做到这一点吗? POCO 论坛中没有充分的支持?
I'm using create process win32 function to start windows shell application for example
Ipconfig and get its output but without popup cmd windows .
Also I trying with the POCO library and its process class but each time the cmd popup.
does any body knows how to do it with the POCO lib there is not mush support in the POCO forums ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要隐藏窗口,请尝试在 lpStartupInfo 上调用 CreateProcess 时传递 SH_HIDE。
如果要收集结果,可以调用CreateFile获取文件句柄,并将该句柄传递到STARTUPINFO结构中,然后在CreateProcess返回时读取它:
To hide thw window, try passing SH_HIDE when calling CreateProcess on lpStartupInfo.
If you want to collect the results, you can call CreateFile to obtain a file handle, and pass the handle in STARTUPINFO structure, then read it when CreateProcess returns:
您可以创建一个没有窗口的 CMD 进程,但您需要为此设置正确的标志,当您创建进程时,您还需要重定向输出。
如果您需要了解 ipconfig 的内容,我建议您浏览 IP 帮助器函数。
You can create a CMD process without a window but you need to set the right flags for this when you create your process you also need to redirect output.
If you need to get at the ipconfig stuff I'd recommen just going through the IP Helper functions of the Win32 API instead.
至少在 Windows 中,从命令提示符中运行的任何内容中提取标准输出都有一些星号。如果您曾经考虑过编写 Windows 命令提示符的替代品,您就会知道。我知道获得精确输出的唯一方法是直接从命令提示符中获取字符。开源项目 Console http://sourceforge.net/projects/console/
在某些情况下,将标准输出的文件句柄更改为您可以读取的文件句柄是可行的。
At least in windows, there are some asterisks around pulling standard output from anything that runs in a command prompt. If you've ever looked into writing a replacement for the windows command prompt you would know. Only way I know of to get precise output is to grab characters directly out of the command prompt. There is an example of this in open source project Console http://sourceforge.net/projects/console/
In some circumstances changing the file handle of standard output to something you can read from will work however.