запускаем внешнее приложение, ждем завершения и получаем вывод

запускаем внешнее консольное приложение и ждем окончания его выполнения, после завершения получаем вывод в список

function RunScript(sCommand: String): TStringList;
var
  OurCommand: String;
  OutputLines: TStringList;
  MemStream: TMemoryStream;
  OurProcess: TProcess;
  NumBytes: LongInt;
  BytesRead: LongInt;
const
  READ_BYTES = 204800;
begin
  MemStream := TMemoryStream.Create;
  BytesRead := 0;
  OurProcess := TProcess.Create(nil);
  OurCommand := sCommand;
  OurProcess.CommandLine := OurCommand;
  OurProcess.ShowWindow:=swoHIDE;
  OurProcess.Options := [poUsePipes, poNoConsole];
  OurProcess.Execute;
  while OurProcess.Running do
  begin
    MemStream.SetSize(BytesRead + READ_BYTES);
    NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
    if NumBytes > 0
    then begin
      Inc(BytesRead, NumBytes);
      Sleep(100);
    end
    else begin
      Sleep(100);
    end;
  end;
  Sleep(1000);
  repeat
    MemStream.SetSize(BytesRead + READ_BYTES);
    NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
    if NumBytes > 0
    then begin
      Inc(BytesRead, NumBytes);
    end;
    Sleep(100);
  until NumBytes <= 0;
  MemStream.SetSize(BytesRead);
  OutputLines := TStringList.Create;
  OutputLines.LoadFromStream(MemStream);
  result := OutputLines;
  OurProcess.Free;
  MemStream.Free;
end;
Поделиться
Отправить
 329   2019   free pascal   lazarus