通过 c++ 运行命令时隐藏控制台窗口

Hide console window while running a command through c++

本文关键字:隐藏 控制台 窗口 命令 c++ 运行 通过      更新时间:2023-10-16

我使用 CreateProcess 运行命令并使用CREATE_NO_WINDOW标志,但控制台弹出了一小部分秒,如何避免?

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
CreateProcess
( 
NULL,                // No module name (use command line)
command,             //set env variable and use it is my command
NULL,                // Process handle not inheritable
NULL,                // Thread handle not inheritable
FALSE,               // Set handle inheritance to FALSE
CREATE_NO_WINDOW,    //don't create window but it appears for fraction of second!
NULL,                // Use parent's environment block
NULL,                // Use parent's starting directory
&si,                 // Pointer to STARTUPINFO structure
&pi                  // Pointer to PROCESS_INFORMATION structure
)  

提前感谢您的帮助。

你必须重定向你的输出。有一个成员hStdOutputhStdError应该重定向。MSDN上就是一个例子。

STARTUPINFO结构中,在dwFlags成员中设置 STARTF_USESHOWWINDOW 标志,并将wShowWindow设置为 SW_HIDE