如何在 Win32 GUI 程序中执行子控制台程序而不显示控制台窗口?

发布于 2024-10-13 02:40:39 字数 487 浏览 5 评论 0原文

(我搜索了 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 技术交流群。

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

发布评论

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

评论(2

向地狱狂奔 2024-10-20 02:40:39

像这样为 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;

Setup the STARTUPINFO like this for the CreateProcess call:

    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;
暮色兮凉城 2024-10-20 02:40:39

CREATE_NO_WINDOW 是关键,在我的测试中,它还将进程与父进程分离,以便子进程在父进程死亡后幸存下来,这是一个僵尸进程

#include <windows.h>  // What would I do without you


STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi;
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

if (!CreateProcess(
    NULL,             // [in, optional]      LPCSTR                lpApplicationName,
    s_command,        // [in, out, optional] LPSTR                 lpCommandLine,
    NULL,             // [in, optional]      LPSECURITY_ATTRIBUTES lpProcessAttributes,
    NULL,             // [in, optional]      LPSECURITY_ATTRIBUTES lpThreadAttributes,
    FALSE,            // [in]                BOOL                  bInheritHandles,
    CREATE_NO_WINDOW, // [in]                DWORD                 dwCreationFlags,
    NULL,             // [in, optional]      LPVOID                lpEnvironment,
    NULL,             // [in, optional]      LPCSTR                lpCurrentDirectory,
    &si,              // [in]                LPSTARTUPINFOA        lpStartupInfo,
    &pi               // [out]               LPPROCESS_INFORMATION lpProcessInformation
    )){
  print("Failed to create process\n");
  return 1;
}

// Wait until child process exits.
// WaitForSingleObject( pi.hProcess, INFINITE );

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
print("Succeded to create process\n");

感谢上面@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.

#include <windows.h>  // What would I do without you


STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi;
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

if (!CreateProcess(
    NULL,             // [in, optional]      LPCSTR                lpApplicationName,
    s_command,        // [in, out, optional] LPSTR                 lpCommandLine,
    NULL,             // [in, optional]      LPSECURITY_ATTRIBUTES lpProcessAttributes,
    NULL,             // [in, optional]      LPSECURITY_ATTRIBUTES lpThreadAttributes,
    FALSE,            // [in]                BOOL                  bInheritHandles,
    CREATE_NO_WINDOW, // [in]                DWORD                 dwCreationFlags,
    NULL,             // [in, optional]      LPVOID                lpEnvironment,
    NULL,             // [in, optional]      LPCSTR                lpCurrentDirectory,
    &si,              // [in]                LPSTARTUPINFOA        lpStartupInfo,
    &pi               // [out]               LPPROCESS_INFORMATION lpProcessInformation
    )){
  print("Failed to create process\n");
  return 1;
}

// Wait until child process exits.
// WaitForSingleObject( pi.hProcess, INFINITE );

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
print("Succeded to create process\n");

Thanks to @9dan comment above.

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