Guest User

Untitled

a guest
Mar 1st, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.10 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Memo1: TMemo;
  12.     Button1: TButton;
  13.     Edit1: TEdit;
  14.     procedure Button1Click(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.   line:string;
  24.  
  25. implementation
  26.  
  27. {$R *.dfm}
  28. function ExecuteConsole(ExeName: string):boolean;
  29. const bufSize = 256;
  30. var readPipe :THandle;
  31. writePipe :THandle;
  32. security :SECURITY_ATTRIBUTES;
  33. info :STARTUPINFO;
  34. process :PROCESS_INFORMATION;
  35.  
  36. buf :array[0..bufSize-1] of char;
  37. bytesRead :DWord;
  38. text :string;
  39.  
  40. newLinePos :integer;
  41.  
  42. progress:string;
  43. ProccessPriority:cardinal;
  44. begin
  45.  
  46.   ProccessPriority:=NORMAL_PRIORITY_CLASS;
  47.  
  48.   result:=FALSE;
  49.   security.nLength:=sizeof(security);
  50.   security.lpSecurityDescriptor:=nil;
  51.   security.bInheritHandle:=TRUE;
  52.   Application.ProcessMessages;
  53.  
  54.   if CreatePipe(readPipe, writePipe, @security, 0) then
  55.   begin
  56.     ZeroMemory(@info, sizeof(info));
  57.     with info do
  58.     begin
  59.       cb := sizeof( info );
  60.       dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  61.       wShowWindow := SW_HIDE;
  62.       hStdInput := 0;
  63.       hStdOutput := writePipe;
  64.       hStdError := writePipe;
  65.     end;
  66.  
  67.   if CreateProcess(nil, PChar(ExeName), nil, nil, TRUE, ProccessPriority, nil, nil, info, process) then
  68.   begin
  69.  
  70.     CloseHandle( writePipe );
  71.  
  72.     text:='';
  73.  
  74.     while ReadFile( readPipe, buf, bufSize, bytesRead, nil) do
  75.     begin
  76.       text:=text+buf;
  77.  
  78.       repeat
  79.  
  80.         newLinePos:=Pos(#13, text);
  81.         if (newLinePos=0) then break;
  82.         line:=copy(text,1, newLinePos-1);
  83.         delete(text, 1, newLinePos);
  84.  
  85.         form1.Memo1.Lines.Add(line);
  86.  
  87.         Application.ProcessMessages;
  88.  
  89.       until Application.Terminated;
  90.  
  91.       ZeroMemory(@buf, bufSize);
  92.     end;
  93.  
  94.     result:=TRUE;
  95.  
  96.   end;
  97.  
  98.     CloseHandle( readPipe );
  99.  
  100.   end;
  101. end;
  102.  
  103. procedure TForm1.Button1Click(Sender: TObject);
  104. begin
  105.     ExecuteConsole(form1.Edit1.Text);
  106.    
  107. end;
  108.  
  109. end.
Advertisement
Add Comment
Please, Sign In to add comment