delphi 管道

使用管道.以下为完整代码.
----------------------------
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
S:TStartupInfo;
P:TProcessInformation;
Sec:TSecurityAttributes;
read,write:THandle;
buffer:array[0..511] of char;
byteread,aprun:DWORD;
begin
Memo1.Lines.Clear;
Label1.Caption:='命令执行中,请等待......';
Sec.nLength:=Sizeof(TSecurityAttributes);
Sec.bInheritHandle:=True;
Sec.lpSecurityDescriptor:=nil;
if not CreatePipe(read,write,@Sec,0) then
raise Exception.Create('create pipe failed');
FillChar(S,Sizeof(TStartupInfo),0);
S.cb:=Sizeof(TStartupInfo);
S.wShowWindow:=SW_HIDE;
S.dwFlags:=STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
S.hStdOutput:=write;
S.hStdError:=write;
if not CreateProcess(nil,Pchar(Edit1.Text),nil,nil,True,NORMAL_PRIORITY_CLASS,nil,nil,S,P) then
Raise Exception.Create('Create Process Failed');
repeat
aprun:=WaitForSingleObject(P.hProcess,250);
Application.ProcessMessages;
until aprun<>WAIT_TIMEOUT;
repeat
byteread:=0;
ReadFile(read,buffer[0],512,byteread,nil);
memo1.Text:=Memo1.Text+string(buffer);
Memo1.Refresh;
until byteread<512;
Label1.Caption:='命令执行完毕。';
CloseHandle(P.hProcess);
CloseHandle(P.hThread);
CloseHandle(read);
CloseHandle(write);
end;
end

你可能感兴趣的:(职场,Delphi,管道,休闲)