C++ Windows CreateChildProcess - 隐藏/不显示子进程的控制台窗口

C++ Windows CreateChildProcess - hide/do not show the child process' console window

本文关键字:子进程 显示 控制台 窗口 Windows CreateChildProcess 隐藏 C++      更新时间:2023-10-16

我需要为我的主过程创建一个子进程作为套接字侦听器/服务器,我使用此调用来实现目标:

bSuccess = CreateProcessA(NULL, 
            cmdLineArgs,   // command line 
            NULL,          // process security attributes 
            NULL,          // primary thread security attributes 
            TRUE,          // handles are inherited 
            HIGH_PRIORITY_CLASS,             // creation flags 
            NULL,          // use parent's environment 
            NULL,          // use parent's current directory 
            &siStartInfo,  // STARTUPINFO pointer 
            &piProcInfo);  // receives PROCESS_INFORMATION 

任何人都可以说出需要做什么才能使孩子流程"不出现"?每次主要的中央进程都会创建一个孩子时,都能有一个可见的过程窗口。

后来编辑我使用了:

HWND hWnd = GetConsoleWindow();
if (hWnd != 0) 
{       
    ShowWindow( hWnd, SW_HIDE);
}

在子过程主要功能中,但这并不是最好的解决方案,因为窗口仍然显示为一秒钟。如果一个人有几个子过程,每个过程都有自己的窗户在屏幕上冒泡,那么它仍然不是优雅的。是否可以为编译器设置任何标志来产生"无机"输出?

我正在使用Visual Studio2010。

CREATE_NO_WINDOW标志仅用于此目的。

您可以将其添加到dwCreationFlags bitmask中:

bSuccess = CreateProcessA(NULL, 
            cmdLineArgs,   // command line 
            NULL,          // process security attributes 
            NULL,          // primary thread security attributes 
            TRUE,          // handles are inherited 
            HIGH_PRIORITY_CLASS | CREATE_NO_WINDOW,  // creation flags 
            NULL,          // use parent's environment 
            NULL,          // use parent's current directory 
            &siStartInfo,  // STARTUPINFO pointer 
            &piProcInfo);  // receives PROCESS_INFORMATION 

您必须使用作为CreateProcess参数提供的STARTUPINFO结构。

STARTUPINFO StartInfo= {sizeof(StartInfo)};
StartInfo.dwFlags= STARTF_USESHOWWINDOW;
StartInfo.wShowWindow= SW_HIDE;
siStartInfo.dwFlags &= STARTF_USESHOWWINDOW;
siStartInfo.wShowWindow = SW_HIDE;

应该做

还请查看http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v = vs.85).aspx