如何在c++中执行shell程序并获取其stout静默不弹出cmd窗口

发布于 2024-11-09 08:47:02 字数 175 浏览 1 评论 0原文

例如,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-11-16 08:47:02

要隐藏窗口,请尝试在 lpStartupInfo 上调用 CreateProcess 时传递 SH_HIDE。

STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;    
CreateProcess(/*...*/, &si /*...*/);

如果要收集结果,可以调用CreateFile获取文件句柄,并将该句柄传递到STARTUPINFO结构中,然后在CreateProcess返回时读取它:

STARTUPINFO si = {0};
si.cb = sizeof(si);
si.hStdOutput = hOutput;
si.hStdError = (HANDLE) STD_ERROR_HANDLE;
si.hStdInput = (HANDLE) STD_INPUT_HANDLE;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
CreateProcess(/*...*/, &si /*...*/);

To hide thw window, try passing SH_HIDE when calling CreateProcess on lpStartupInfo.

STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;    
CreateProcess(/*...*/, &si /*...*/);

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:

STARTUPINFO si = {0};
si.cb = sizeof(si);
si.hStdOutput = hOutput;
si.hStdError = (HANDLE) STD_ERROR_HANDLE;
si.hStdInput = (HANDLE) STD_INPUT_HANDLE;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
CreateProcess(/*...*/, &si /*...*/);
清风不识月 2024-11-16 08:47:02

您可以创建一个没有窗口的 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.

≈。彩虹 2024-11-16 08:47:02

至少在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文