如何在 Win32 GUI 程序中执行子控制台程序而不显示控制台窗口?
(我搜索了 SO 答案,但没有找到解决此问题的明确解决方案。)
我正在开发 MFC GUI 程序。该程序运行各种子程序,包括控制台程序和shell命令脚本(.cmd)。
最初它显示一个 GUI 窗口和一个控制台窗口(使用 AllocConsole
创建),因为子进程有许多控制台输出。但许多用户抱怨控制台窗口,因此我们决定隐藏控制台窗口。
首先尝试如下:
if (AllocConsole())
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
好的,没有控制台窗口,但在控制台创建时有明显的闪烁。 我已经尝试了几个用于创建子进程的 CreateProcess 选项,以防止完全显示控制台窗口,但很快就失败了,我认为这实际上是不可能的。
这没什么大不了的。我们可以忽略启动时的临时窗口闪烁。
但是完全隐藏子控制台窗口真的不可能吗?
(I've searched SO answers and found no clear solution to this problem.)
I'm working on a MFC GUI program. This program runs various child programs including console program and shell command script(.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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样为 CreateProcess 调用设置 STARTUPINFO:
Setup the STARTUPINFO like this for the CreateProcess call:
CREATE_NO_WINDOW 是关键,在我的测试中,它还将进程与父进程分离,以便子进程在父进程死亡后幸存下来,这是一个僵尸进程。
感谢上面@9dan 的评论。
CREATE_NO_WINDOW is the key, in my tests, it also detaches the process from the parent so that the child survives the parent death, a Zombie process.
Thanks to @9dan comment above.