Einzelnen Beitrag anzeigen

Benutzerbild von Matze
Matze
(Co-Admin)

Registriert seit: 7. Jul 2003
Ort: Schwabenländle
14.929 Beiträge
 
Turbo Delphi für Win32
 
#3

Re: DOS Befehle ausführen/rückantworten

  Alt 15. Dez 2004, 22:28
Probier's mal so:

{
Delphi-Quellcode:
procedure TForm1.CaptureDosCmd(command:String; OutputMemo:TMemo);
const
  CaptureBufferSize = 2500;
var
  SecAttrib : TSecurityAttributes;
  ReadPipe,writePipe: THandle;
  Startup : TStartUpInfo;
  ProcessInfo : TProcessInformation;
  CaptureBuffer : Pchar;
  BytesRead : DWord;
  WaitHandle : DWord;

begin
  OutPutMemo.Lines.clear;
  //OutputMemo.lines.add('executing "' + command + '"');
  With SecAttrib do
  begin
    nlength := SizeOf(TSecurityAttributes);
    binherithandle := true;
    lpsecuritydescriptor := nil;
  end;

  if Createpipe (ReadPipe, writePipe, @SecAttrib, 0) then
  begin
    CaptureBuffer := AllocMem(CaptureBufferSize + 1);
    FillChar(Startup,Sizeof(Startup),#0);
    Startup.cb := SizeOf(Startup);
    Startup.hStdOutput := writePipe;
    Startup.hStdInput := ReadPipe;
    Startup.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    Startup.wShowWindow := SW_HIDE;
    if CreateProcess(nil, PChar(command), @SecAttrib, @SecAttrib, true, NORMAL_PRIORITY_CLASS, nil, nil, Startup, ProcessInfo) then
    begin
      repeat
        WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,100);
        Application.ProcessMessages;
      until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;

      if not application.terminated then
      Repeat
        BytesRead := 0;
        ReadFile(ReadPipe,CaptureBuffer[0],CaptureBufferSize,BytesRead,nil);
        CaptureBuffer[BytesRead]:= #0;
        OemToAnsi(CaptureBuffer,CaptureBuffer);
        OutputMemo.Text := OutputMemo.Text+String(CaptureBuffer);
        Application.ProcessMessages;
      until (BytesRead < CaptureBufferSize);
    end
    else OutPutMemo.lines.add('Operation wurde abgebrochen!');

    FreeMem(CaptureBuffer);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ReadPipe);
    CloseHandle(writePipe);
  end
  else OutPutMemo.lines.add('Konnte Kommando nicht starten, Fehler: #'+ inttostr(getlasterror));
end;
Falls das noch nicht in der Code-Library ist, und es geht, kann ich's dann reingeben.
  Mit Zitat antworten Zitat