gpt4 book ai didi

c - 使用 CreateProcess,无法获取 CREATE_NO_WINDOW 来抑制控制台

转载 作者:可可西里 更新时间:2023-11-01 12:03:57 25 4
gpt4 key购买 nike

我想在不弹出控制台窗口的情况下启动一个进程(为简单起见,我们将使用记事本)。

我确定我错过了一些非常简单的东西,这是我最简化的测试用例:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
"notepad", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

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

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}

最佳答案

您正在从父进程(您的控制台应用程序)创建一个新进程(notepad.exe),并让父进程等待子进程完成。控制台窗口是父进程的主窗口。您可以隐藏和恢复如下所示。

// Notice how hiding the console window causes it to disappear from
// the Windows task bar. If you only want to make it minimize, use
// SW_MINIMIZE instead of SW_HIDE.
void _tmain(int argc, TCHAR *argv[])
{
ShowWindow( GetConsoleWindow(), SW_HIDE );

// create a new process and wait for it to finish

ShowWindow( GetConsoleWindow(), SW_RESTORE );
}

关于c - 使用 CreateProcess,无法获取 CREATE_NO_WINDOW 来抑制控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7116328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com