简体   繁体   English

如何在不显示Win32 GUI程序的控制台窗口的情况下执行子控制台程序?

[英]How to execute child console programs without showing the console window from the Win32 GUI program?

(I've searched SO answers and found no clear solution to this problem.) (我搜索了SO的答案,发现这个问题没有明确的解决方案。)

I'm working on a MFC GUI program. 我正在研究MFC GUI程序。 This program runs various child programs including console program and shell command script(.cmd). 该程序运行各种子程序,包括控制台程序和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. 最初它显示了一个GUI窗口和一个控制台窗口(使用AllocConsole创建),因为子进程有许多控制台输出。 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. 我已经为子进程创建尝试了几个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? 但完全隐藏儿童游戏机窗口真的不可能吗?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM