Delphi: wait until bat-script runs to the end

有些话、适合烂在心里 提交于 2019-12-19 09:07:03

问题


I have bat-file, that make some operations. How to run this file from Delphi and wait, until it stops. Something like that:

procedure TForm1.Button1Click(Sender: TObject);
begin
//Starting bat-file
bla-bla-bla
showmessage('Done');
end;

回答1:


This executes the given command line and waits for the program started by the command line to exit. Returns true if the program returns a zero exit code and false if the program doesn't start or returns a non-zero error code.

function ExecAndWait(const CommandLine: string) : Boolean;
var
  StartupInfo: Windows.TStartupInfo;        // start-up info passed to process
  ProcessInfo: Windows.TProcessInformation; // info about the process
  ProcessExitCode: Windows.DWord;           // process's exit code
begin
  // Set default error result
  Result := False;
  // Initialise startup info structure to 0, and record length
  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  StartupInfo.cb := SizeOf(StartupInfo);
  // Execute application commandline
  if Windows.CreateProcess(nil, PChar(CommandLine),
    nil, nil, False, 0, nil, nil,
    StartupInfo, ProcessInfo) then
  begin
    try
      // Now wait for application to complete
      if Windows.WaitForSingleObject(ProcessInfo.hProcess, INFINITE)
        = WAIT_OBJECT_0 then
        // It's completed - get its exit code
        if Windows.GetExitCodeProcess(ProcessInfo.hProcess,
          ProcessExitCode) then
          // Check exit code is zero => successful completion
          if ProcessExitCode = 0 then
            Result := True;
    finally
      // Tidy up
      Windows.CloseHandle(ProcessInfo.hProcess);
      Windows.CloseHandle(ProcessInfo.hThread);
    end;
  end;
end;

From: http://www.delphidabbler.com/codesnip?action=named&showsrc=1&routines=ExecAndWait




回答2:


Here is some code and example - under Windows 7 works fine and is invisible

(funcion ExeAndWait is borrowed).

function ExeAndWait(ExeNameAndParams: string; ncmdShow: Integer = SW_SHOWNORMAL): Integer;
var
    StartupInfo: TStartupInfo;
    ProcessInformation: TProcessInformation;
    Res: Bool;
    lpExitCode: DWORD;
begin
    with StartupInfo do //you can play with this structure
    begin
        cb := SizeOf(TStartupInfo);
        lpReserved := nil;
        lpDesktop := nil;
        lpTitle := nil;
        dwFlags := STARTF_USESHOWWINDOW;
        wShowWindow := ncmdShow;
        cbReserved2 := 0;
        lpReserved2 := nil;
    end;
    Res := CreateProcess(nil, PChar(ExeNameAndParams), nil, nil, True,
        CREATE_DEFAULT_ERROR_MODE
        or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInformation);
    while True do
    begin
        GetExitCodeProcess(ProcessInformation.hProcess, lpExitCode);
        if lpExitCode <> STILL_ACTIVE then
            Break;
        Application.ProcessMessages;
    end;
    Result := Integer(lpExitCode);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    ExeAndWait(ExtractFilePath(Application.ExeName) + 'test.bat', SW_HIDE);
    ShowMessage('Done!');
end;

PS. If you like you can build batch file at runtime using TStringList class.



来源:https://stackoverflow.com/questions/2443886/delphi-wait-until-bat-script-runs-to-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!