我正在使用提供给我的程序,它将运行一个进程,但我希望该进程在后台运行而不显示窗口.电话是
ExecProcess(ProgPath,'',False);
而且功能是
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 or NORMAL_PRIORITY_CLASS,StartInfo,ProcInfo); end else begin CreateOK := CreateProcess(nil,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;
我试图使用SW_MINIMIZE,SW_FORCEMINIMIZE和SW_SHOWMINIMIZED的StartInfo.wShowWindow属性,但它无法正常工作.我可以看到调试器中的属性正在发生变化,但就像我现在所理解的那样.有人能指出问题是什么吗?
编辑:如果重要我正在执行一些Fortran模块(.exe),其参数将打开一个CMD窗口.
解决方法
使用带有STARTF_USESHOWWINDOW的dwFlags强制使用wShowWindow
StartInfo.wShowWindow := SW_HIDE; StartInfo.dwFlags := STARTF_USESHOWWINDOW;