Autor Beitrag
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 05.09.06 22:45 
Wie kann man ein Konsolen-Kommando ausführen und das Ergebnis zurückliefern?
Update auf v2.00 (Code-Archiv)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
// Konsolen-kommando ausführen und Ergebnis/Fehler zurückliefern
function ExecConsoleCommand(const ACommand: Stringvar AOutput, AErrors: String;
                            var AExitCode: Cardinal): Boolean;
  var
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    SecurityAttr: TSecurityAttributes;
    PipeOutputRead, PipeOutputWrite,
    PipeErrorsRead, PipeErrorsWrite: THandle;

  // Pipe in einen String auslesen
  procedure ReadPipeToString(const hPipe: THandle; var Result: String);
    var
      AvailableBytes,
      ReadBytes: Cardinal;
      Buffer: String;
  begin
    PeekNamedPipe(hPipe, NIL0NIL, @AvailableBytes, NIL); // wieviel ist da
    while (AvailableBytes > 0do begin // überhaupt was da?
      SetLength(Buffer,AvailableBytes); // Buffer
      if ReadFile(hPipe,PChar(Buffer)^,AvailableBytes,ReadBytes,NILthen // Lesen hat geklappt
        if (ReadBytes > 0then begin // Daten angekommen?
          SetLength(Buffer,ReadBytes); // falls weniger kam, als da ist (warum auch immer)
          Result := Result +Buffer; // an den Ergebnis-String
        end;
      PeekNamedPipe(hPipe, NIL0NIL, @AvailableBytes, NIL); // noch was da?
    end;
  end;

begin
  AOutput := '';
  AErrors := '';
  // Win-API-Strukturen initialisieren/füllen
  FillChar(ProcessInfo,SizeOf(TProcessInformation),0);
  FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := TRUE;
  SecurityAttr.lpSecurityDescriptor := NIL;
  CreatePipe(PipeOutputRead,PipeOutputWrite,@SecurityAttr,0);
  CreatePipe(PipeErrorsRead,PipeErrorsWrite,@SecurityAttr,0);
  FillChar(StartupInfo,SizeOf(TStartupInfo),0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := SW_HIDE;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  // http://msdn2.microsoft.com/en-us/library/ms682425.aspx
  Result := CreateProcess(NIL,PChar(ACommand),NIL,NIL,TRUE,
                          CREATE_DEFAULT_ERROR_MODE
                          or CREATE_NEW_CONSOLE
                          or NORMAL_PRIORITY_CLASS,
                          NIL,NIL,StartupInfo,ProcessInfo);
  if Result then begin // Prozess erfolgreich gestartet?
    repeat
      GetExitCodeProcess(ProcessInfo.hProcess,AExitCode); // http://msdn2.microsoft.com/en-us/library/ms683189.aspx
      ReadPipeToString(PipeOutputRead,AOutput); // Ausgaben lesen
      ReadPipeToString(PipeErrorsRead,AErrors); // Fehler lesen
      if (AExitCode = STILL_ACTIVE) then
        Sleep(1);
    until (AExitCode <> STILL_ACTIVE); // bis der Prozess sich selbst beendet hat
    CloseHandle(ProcessInfo.hThread); // Handles freigeben
    CloseHandle(ProcessInfo.hProcess); 
  end;
  CloseHandle(PipeOutputWrite); // Pipes schließen
  CloseHandle(PipeErrorsWrite);
  CloseHandle(PipeOutputRead);
  CloseHandle(PipeErrorsRead);
end;

Im Anhang befindet sich ein komplettes Testprojekt, das die Verwendung der Funktion erläutert. Dort ist die Funktion in einer separaten Unit ExecConsole.pas gekapselt, die auch noch eine TThread-basierte Klasse enthält, um das Kommando asynchron ausführen zu können (damit die GUI nicht blockiert).
Neu in v2.0: Timeoutzeit für die Ausführung, mit optionalem Prozess-Abbruch bei Überschreitung.


ConsoleCommand.zip  (5.1 KB) Download (Rev 2)
 (642x, 1331x gesamt)
Beschreibung: Quelltext der Unit ExecConsole.pas und des Demo-Programms, v2.0
CmdExec.exe  (149 KB) Download (Rev 1)
 (700x, 1028x gesamt)
Beschreibung: Demo-Programm (UPX!)
_________________
There are 10 types of people - those who understand binary and those who don´t.