winapi:CreateProcess但隐藏进程的窗口?

我正在使用CreateProcess来创build一个cmd.exe进程,它传递一个参数,它将执行并退出,这使得命令提示符在屏幕上闪烁。

我试图通过将STARTUPINFO结构wShowWindow设置为SW_HIDE来避免这种情况,但此参数似乎影响调用窗口,而不是执行进程的窗口。

有无论如何,你可以使用createprocess启动一个隐藏的视图的程序?

另外什么是适当的WINAPI标准的方式来获得环境variables?

如果它只是一个控制台应用程序,也可以使用CREATE_NO_WINDOW标志作为CreateProcess调用本身的一部分,例如

 CreateProcess(NULL, lpszCommandLine, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); 

此外,请参阅此页面了解有关环境变量的信息。

在dwFlags中设置STARTF_USESHOWWINDOW

通过锐利

以下链接介绍如何静默地创建窗口:

 DWORD RunSilent(char* strFunct, char* strstrParams) { STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; char Args[4096]; char *pEnvCMD = NULL; char *pDefaultCMD = "CMD.EXE"; ULONG rc; memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESHOWWINDOW; StartupInfo.wShowWindow = SW_HIDE; Args[0] = 0; pEnvCMD = getenv("COMSPEC"); if(pEnvCMD){ strcpy(Args, pEnvCMD); } else{ strcpy(Args, pDefaultCMD); } // "/c" option - Do the command then terminate the command window strcat(Args, " /c "); //the application you would like to run from the command window strcat(Args, strFunct); strcat(Args, " "); //the parameters passed to the application being run from the command window. strcat(Args, strstrParams); if (!CreateProcess( NULL, Args, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo)) { return GetLastError(); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); return rc; } 

我觉得getenv和setenv都可以吗? 我不确定你在这方面的问题。

这可能是一个矫枉过正的需求,但你可以钩ShowWindow API,并不会显示任何窗口的过程