使用' CreateProcess'隐藏流程窗口

时间:2014-11-06 09:26:21

标签: delphi delphi-xe5

我使用提供给我的程序来运行一个进程,但我希望该进程在后台运行,而不会显示该窗口。电话是

 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, nil, nil,
      StartInfo, ProcInfo);
  end
  else
  begin
    CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
      CREATE_NEW_PROCESS_GROUP or 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;

我尝试将StartInfo.wShowWindow属性与SW_MINIMIZESW_FORCEMINIMIZESW_SHOWMINIMIZED一起使用,但它无法使用。我可以看到调试器中的属性正在发生变化,但现在我已经了解了。有人能指出问题是什么吗?

编辑:如果重要的是我执行了几个带有参数的Fortran模块(.exe),这些参数将打开一个CMD窗口。

1 个答案:

答案 0 :(得分:9)

将dwFlags与STARTF_USESHOWWINDOW一起使用以强制使用wShowWindow

StartInfo.wShowWindow := SW_HIDE;
StartInfo.dwFlags := STARTF_USESHOWWINDOW;