Einzelnen Beitrag anzeigen

Benutzerbild von mirage228
mirage228

Registriert seit: 23. Mär 2003
Ort: Münster
3.750 Beiträge
 
Delphi 2010 Professional
 
#4

Re: "In Konsole arbeiten" - Funktion tuts nicht im

  Alt 5. Feb 2005, 22:27
Hi,

ich verwende diese Funktion, um den Konsolenoutput zurückzubekommen:
Delphi-Quellcode:
// based on [url]http://www.swissdelphicenter.com/de/showcode.php?id=990[/url]
function RunCaptured(const DirName, ExeName, CmdLine: string;
  OutPut: TStrings; var ReturnCode: Cardinal): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  FileName: string;
  FileHandle: THandle;
  SecurityAttributes: TSecurityAttributes;
begin
  Result := False;
  try
    // set a temporary file
    FileName := 'Test.tmp';
    FillChar(SecurityAttributes, SizeOf(SecurityAttributes), #0);
    SecurityAttributes.nLength := SizeOf(SecurityAttributes);
    SecurityAttributes.bInheritHandle := True;
    FileHandle := Windows.CreateFile(PChar(FileName),
      GENERIC_WRITE, FILE_SHARE_WRITE, @SecurityAttributes, CREATE_ALWAYS,
      FILE_ATTRIBUTE_NORMAL, 0);
    try
      FillChar(StartupInfo, SizeOf(StartupInfo), #0);
      StartupInfo.cb := SizeOf(StartupInfo);
      StartupInfo.hStdOutput := FileHandle;
      StartupInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_HIDE;
      // start the program
      if CreateProcess(nil, PChar(ExeName + ' ' + CmdLine), nil, nil, True,
        0, nil, PChar(DirName), StartupInfo, ProcInfo) then
      begin
        SetPriorityClass(ProcInfo.hProcess, IDLE_PRIORITY_CLASS);
        WaitForSingleObject(ProcInfo.hProcess, Infinite);
        GetExitCodeProcess(ProcInfo.hProcess, ReturnCode);
        // PHP.exe returns 255 on parse errors!
        Result := True;
        CloseHandle(ProcInfo.hThread);
        CloseHandle(ProcInfo.hProcess);
        Windows.CloseHandle(FileHandle);
        // Add the output
        if (OutPut <> nil) then
          OutPut.LoadFromFile(FileName);
      end;
      Windows.DeleteFile(PChar(FileName));
    except
      Windows.CloseHandle(FileHandle);
      Windows.DeleteFile(PChar(FileName));
      Result := False;
    end;
  finally
  end;
end;
Vielleichgt hilfts Dir ja

mfG
mirage228
David F.

May the source be with you, stranger.
PHP Inspection Unit (Delphi-Unit zum Analysieren von PHP Code)
  Mit Zitat antworten Zitat