windows 如何在不显示 Win32 GUI 程序的控制台窗口的情况下执行子控制台程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4743559/
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
How to execute child console programs without showing the console window from the Win32 GUI program?
提问by 9dan
(I've searched SO answers and found no clear solution to this problem.)
(我已经搜索了 SO 答案,但没有找到解决此问题的明确方法。)
I'm working on a MFC GUI program. This program runs various child programs including console program and shell command script(.cmd).
我正在开发 MFC GUI 程序。该程序运行各种子程序,包括控制台程序和shell 命令脚本(.cmd)。
Initially it displayed one GUI window and one console window (created with AllocConsole
) because there are many console output from the child processes. But many users complained about the console window so we decided to hide the console window.
最初它显示一个 GUI 窗口和一个控制台窗口(使用AllocConsole
),因为子进程有许多控制台输出。但是很多用户抱怨控制台窗口,所以我们决定隐藏控制台窗口。
Firstly tried like below:
首先尝试如下:
if (AllocConsole())
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
Okay, no console window but there are visible flicker at the console creation time.
I've tried several CreateProcess
options for child process creation to prevent showing of console window altogether but failed at short and I think it is practically impossible.
好的,没有控制台窗口,但在控制台创建时有可见的闪烁。我已经尝试了几种CreateProcess
创建子进程的选项,以防止完全显示控制台窗口,但总之失败了,我认为这实际上是不可能的。
It is not a big deal. We can ignore temporary window flicker at the startup.
这没什么大不了的。我们可以忽略启动时的临时窗口闪烁。
But is it really impossible to hide child console window completely?
但是真的不可能完全隐藏子控制台窗口吗?
回答by John
Setup the STARTUPINFO like this for the CreateProcess call:
像这样为 CreateProcess 调用设置 STARTUPINFO:
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.wShowWindow = SW_HIDE;