Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- Button1: TButton;
- Edit1: TEdit;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- line:string;
- implementation
- {$R *.dfm}
- function ExecuteConsole(ExeName: string):boolean;
- const bufSize = 256;
- var readPipe :THandle;
- writePipe :THandle;
- security :SECURITY_ATTRIBUTES;
- info :STARTUPINFO;
- process :PROCESS_INFORMATION;
- buf :array[0..bufSize-1] of char;
- bytesRead :DWord;
- text :string;
- newLinePos :integer;
- progress:string;
- ProccessPriority:cardinal;
- begin
- ProccessPriority:=NORMAL_PRIORITY_CLASS;
- result:=FALSE;
- security.nLength:=sizeof(security);
- security.lpSecurityDescriptor:=nil;
- security.bInheritHandle:=TRUE;
- Application.ProcessMessages;
- if CreatePipe(readPipe, writePipe, @security, 0) then
- begin
- ZeroMemory(@info, sizeof(info));
- with info do
- begin
- cb := sizeof( info );
- dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
- wShowWindow := SW_HIDE;
- hStdInput := 0;
- hStdOutput := writePipe;
- hStdError := writePipe;
- end;
- if CreateProcess(nil, PChar(ExeName), nil, nil, TRUE, ProccessPriority, nil, nil, info, process) then
- begin
- CloseHandle( writePipe );
- text:='';
- while ReadFile( readPipe, buf, bufSize, bytesRead, nil) do
- begin
- text:=text+buf;
- repeat
- newLinePos:=Pos(#13, text);
- if (newLinePos=0) then break;
- line:=copy(text,1, newLinePos-1);
- delete(text, 1, newLinePos);
- form1.Memo1.Lines.Add(line);
- Application.ProcessMessages;
- until Application.Terminated;
- ZeroMemory(@buf, bufSize);
- end;
- result:=TRUE;
- end;
- CloseHandle( readPipe );
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- ExecuteConsole(form1.Edit1.Text);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment