Einzelnen Beitrag anzeigen

jziersch

Registriert seit: 9. Okt 2003
Ort: München
251 Beiträge
 
Delphi 10.4 Sydney
 
#9

AW: Kommandozeilenprogramm ausführen und Ausgabe umleiten

  Alt 12. Jul 2018, 07:50
Ich habe diese Funktion eingesetzt um die Ausgabe von DCRaw in einen Stream umzuleiten.

Code:
function GetDosOutput(CommandLine: string; Params: string; AStream : TStream): Integer;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir, AParameter: string;
  Handle: Boolean;
begin
  Result := 0;
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := 'C:\';
    AParameter := Format('"%s" %s', [CommandLine, TrimRight(Params)]);

    Handle := CreateProcess(nil, PChar({'cmd.exe /C ' + }AParameter),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            AStream.Write(Buffer[0],BytesRead);
            Result := Result + BytesRead;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;
vgl: http://stackoverflow.com/questions/9...o-a-delphi-app

Grüsse,
Julian
WPCubed GmbH
Komponenten für Delphi:
WPTools, wPDF, WPViewPDF
  Mit Zitat antworten Zitat