Einzelnen Beitrag anzeigen

Nightshade

Registriert seit: 7. Jan 2003
Ort: Menden
192 Beiträge
 
Delphi 7 Enterprise
 
#6

Re: Konsole nachproggen - kleine Schwierigkeiten

  Alt 4. Sep 2004, 03:50
Ich hab da hier im Forum irgendwo mal diese Function gefunden,
vielleicht hilft dir das weiter :

Delphi-Quellcode:
procedure TEditForm.CaptureDosCmd(command, Params:String; var OutputMemo:TStringList);
const
  CaptureBufferSize = 4096;
var
  SecAttrib : TSecurityAttributes;
  ReadPipe,writePipe : THandle;
  Startup : TStartUpInfo;
  ProcessInfo : TProcessInformation;
  CaptureBuffer : Pchar;
  BytesRead : DWord;
  WaitHandle : DWord;
  CurSize : Cardinal;
  procedure GetBuffer;
  begin
      Repeat
        BytesRead := 0;
        ReadFile(ReadPipe,CaptureBuffer[0],CaptureBufferSize,BytesRead,nil);
        CaptureBuffer[BytesRead]:= #0;
        OemToAnsi(CaptureBuffer,CaptureBuffer);
        OutputMemo.Text := OutputMemo.Text+String(CaptureBuffer);
      until (BytesRead < CaptureBufferSize);
  end;

begin
  OutPutMemo.clear;
// OutputMemo.add('# Starte "'+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;
// Startup.wShowWindow := SW_NORMAL;
    if CreateProcess(PChar(command), PChar(params), @SecAttrib, @SecAttrib, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, Startup, ProcessInfo) then begin
      repeat
        WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,0);

        CurSize := FileSeek(ReadPipe,0,2);
        If (CurSize >= 4096) then // Puffer in Memo ausgeben und damit leeren
          GetBuffer;
        Application.ProcessMessages;
      until (WaitHandle = WAIT_OBJECT_0) or (WaitHandle = WAIT_FAILED) or application.terminated;

      if not application.terminated then begin
        CurSize := FileSeek(ReadPipe,0,2);
        If (CurSize > 0 ) then // Falls Rest im Puffer, in Memo ausgeben
          GetBuffer;
      end
      else
        OutPutMemo.add('# Operation canceled!');
      FreeMem(CaptureBuffer);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      CloseHandle(ReadPipe);
      CloseHandle(writePipe);
    end
    else
      OutPutMemo.add('# cannot create process. Error: #'+inttostr(getlasterror));
  end
  else
    OutPutMemo.add('# cannot create pipe. Error: #'+inttostr(getlasterror));
end;
Christian
Killing for peace is like fucking for virginity

Nightshade
  Mit Zitat antworten Zitat