Hide the process window using "CreateProcess"

I use the procedure provided to me that will start the process, but I want the process to run in the background without a window appearing. Call

ExecProcess(ProgPath, '', False); 

and function

 function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer; var StartInfo: TStartupInfo; ProcInfo: TProcessInformation; CreateOK: boolean; ExitCode: integer; dwExitCode: DWORD; begin ExitCode := -1; FillChar(StartInfo, SizeOf(TStartupInfo), #0); FillChar(ProcInfo, SizeOf(TProcessInformation), #0); StartInfo.cb := SizeOf(TStartupInfo); if WorkDir <> '' then begin CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]), false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, nil, StartInfo, ProcInfo); end else begin CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]), StartInfo, ProcInfo); end; { check to see if successful } if CreateOK then begin // may or may not be needed. Usually wait for child processes if Wait then begin WaitForSingleObject(ProcInfo.hProcess, INFINITE); GetExitCodeProcess(ProcInfo.hProcess, dwExitCode); ExitCode := dwExitCode; end; end else begin ShowMessage('Unable to run ' + ProgramName); end; CloseHandle(ProcInfo.hProcess); CloseHandle(ProcInfo.hThread); Result := ExitCode; end; 

I tried using the StartInfo.wShowWindow attribute with SW_MINIMIZE , SW_FORCEMINIMIZE and SW_SHOWMINIMIZED , but it does not work. I see that the attribute is changing in the debugger, but as far as I understand it right now. Can anyone indicate what the problem is?

EDIT: If this is important, I execute a couple of Fortran modules (.exe) with arguments that open a CMD window.

+5
source share
1 answer

Use dwFlags with STARTF_USESHOWWINDOW to force use of wShowWindow

 StartInfo.wShowWindow := SW_HIDE; StartInfo.dwFlags := STARTF_USESHOWWINDOW; 
+7
source

Source: https://habr.com/ru/post/1206330/


All Articles