Einzelnen Beitrag anzeigen

Patrick

Registriert seit: 15. Sep 2003
184 Beiträge
 
Delphi 2010 Professional
 
#1

Problem mit CreateProcess

  Alt 25. Aug 2005, 09:45
Hallo,

Ich verwende die folgende Funktion, um einen CMD-Befehl auszuführen und die Rückmeldung abzufangen.

Delphi-Quellcode:
function GetConsoleOutput(const Command: String; var Output, Errors: TStringList): Boolean;
var StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeOutputRead: THandle;
  PipeOutputWrite: THandle;
  PipeErrorsRead: THandle;
  PipeErrorsWrite: THandle;
  Succeed: Boolean;
  Buffer: array [0..255] of Char;
  NumberOfBytesRead: DWORD;
  Stream: TMemoryStream;
begin
  //ShowMessage(command);
  //Initialisierung ProcessInfo
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
  //Initialisierung SecurityAttr
  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := true;
  SecurityAttr.lpSecurityDescriptor := nil;
  //Pipes erzeugen CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0);
  CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0);
  //Initialisierung StartupInfo
  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;
  if CreateProcess(nil, PChar(command), nil, nil, true,
  CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil,
  StartupInfo, ProcessInfo) then
  begin
    result:=true;
    //Write-Pipes schließen
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsWrite);
    //Ausgabe Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do
      begin
        succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Output.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeOutputRead);
    //Fehler Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do
      begin
        succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Errors.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeErrorsRead);
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
  end
  else
  begin
    result:=false;
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsRead);
    CloseHandle(PipeErrorsWrite);
  end;
end:
Diesen Befehl gebe in in Command ein:
cmd /c "C:\Programme\MySQL\MySQL Server 4.1\bin\mysqldump.exe" -hlocalhost -ubenutzer -ppasswort --opt "web1db1" > "D:\download\2005-08-24 13-27\web1db1.sql"

Allerdings ist die Rückmeldung weniger schön:
Der Befehl "C:\Programme\MySQL\MySQL" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

Er rafft nicht, dass er die ganze Zeile bearbeiten soll. Wenn ich exact diese Zeile (Ohne das "cmd /c ") in die Eingabeaufforderung eingebe funktioniert es.

Lasse ich im Command meines Programmes das "cmd /c " weg gibt's diese Meldung:
mysqldump: Couldn't find table: ">"
Was auch schwachsinn ist, denn der Befehl ansich ist richtig!

Also wo liegt der Fehler? Bin ich es oder die Api CreateProcess?

[edit=SirThornberry]Neu abgespeichert um Higlighting zu korrigieren. Mfg, SirThornberry[/edit]
Genieße jede Minute deines Lebens, denn sie wird nicht wieder kommen.
  Mit Zitat antworten Zitat