Search results for 'TStartUpInfo'. 1 post(s) found.

  1. 2007/09/19 How to capture the output from a DOS (command/console) Window
2007/09/19 08:09

How to capture the output from a DOS (command/console) Window


The example runs 'chkdsk.exe c:\' and displays the output to Memo1.
Put a TMemo (Memo1) and a TButton (Button1) on your form. Put this code in the OnCLick event procedure for Button1:

procedure TForm1.Button1Click(Sender: TObject) ;

  procedure RunDosInMemo(DosApp:String;AMemo:TMemo) ;
  const
     ReadBuffer = 2400;
  var
   Security : TSecurityAttributes;
   ReadPipe,WritePipe : THandle;
   start : TStartUpInfo;
   ProcessInfo : TProcessInformation;
   Buffer : Pchar;
   BytesRead : DWord;
   Apprunning : DWord;
  begin
   With Security do begin
    nlength := SizeOf(TSecurityAttributes) ;
    binherithandle := true;
    lpsecuritydescriptor := nil;
   end;
   if Createpipe (ReadPipe, WritePipe, @Security, 0) then begin
    Buffer := AllocMem(ReadBuffer + 1) ;
    FillChar(Start,Sizeof(Start),#0) ;
    start.cb := SizeOf(start) ;
    start.hStdOutput := WritePipe;
    start.hStdInput := ReadPipe;
    start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    start.wShowWindow := SW_HIDE;

    if CreateProcess(nil,
           PChar(DosApp),
           @Security,
           @Security,
           true,
           NORMAL_PRIORITY_CLASS,
           nil,
           nil,
           start,
           ProcessInfo)
    then
    begin
     repeat
      Apprunning := WaitForSingleObject
                   (ProcessInfo.hProcess,100) ;
      Application.ProcessMessages;
     until (Apprunning <> WAIT_TIMEOUT) ;
      Repeat
        BytesRead := 0;
        ReadFile(ReadPipe,Buffer[0], ReadBuffer,BytesRead,nil) ;
        Buffer[BytesRead]:= #0;
        OemToAnsi(Buffer,Buffer) ;
        AMemo.Text := AMemo.text + String(Buffer) ;
      until (BytesRead < ReadBuffer) ;
   end;
   FreeMem(Buffer) ;
   CloseHandle(ProcessInfo.hProcess) ;
   CloseHandle(ProcessInfo.hThread) ;
   CloseHandle(ReadPipe) ;
   CloseHandle(WritePipe) ;
   end;
  end;

  begin {button 1 code}
    RunDosInMemo('chkdsk.exe c:\',Memo1) ;
  end;
Trackback 5 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Horse porn beast porn dog porn animal porn zoo.

    Tracked from Horse porn beast porn dog porn animal porn. 2009/03/31 19:26 delete

    Zoo sex pictures animal porn movies beast trailers.

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/25 08:31 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 21:49 delete

    moneyideas

  4. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 06:17 delete

    moneyideas

  5. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:40 delete

    moneyideas