YYSUN:谢谢,NOV 14,我记住了。<br><br>NETWIND:对,我觉得ALLOCCONSOLE也是为了重新定向它的输入/输出,YYSUN给的VB代码<br>我也尝试用DELPHI写了,在WINDOW95/98环境下,这段代码运行到:<br> ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, nil);<br><br>是会死掉。<br><br>代码如下<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> proc : PROCESS_INFORMATION;<br> start : STARTUPINFO;<br> sa : SECURITY_ATTRIBUTES;<br> psa: PSecurityAttributes;<br> hReadPipe : THandle;<br> hWritePipe : THandle;<br> sOutput : String;<br> lngBytesRead : dword;<br> ret : boolean;<br> sBuffer : Array [0..255] of char;<br><br>begin<br> sa.nLength := sizeof(TSECURITYATTRIBUTES);<br> sa.bInheritHandle := True;<br> sa.lpSecurityDescriptor := nil;<br> psa := @sa;<br> ret := CreatePipe(hReadPipe, hWritePipe, psa, 0);<br> If not ret Then begin<br> showmessage('pipe create err');<br> exit;<br> end;<br> FillChar(Start, Sizeof(Start), #0);<br> start.cb := SizeOf(start);<br> start.dwFlags := STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW;<br> start.hStdOutput := hWritePipe;<br> start.hStdError := hWritePipe;<br> start.wShowWindow := SW_hide;<br>//CreateProcess(nil, PChar('C:/tmp/console1/project1.exe'), sa, sa, True,<br> // NORMAL_PRIORITY_CLASS,<br> // nil, nil, start, proc);<br><br> ret := CreateProcessA(nil, PChar('C:/tmp/console1/project1.exe'), psa, psa, true, NORMAL_PRIORITY_CLASS, nil, nil, Start, Proc);<br> If not ret Then begin<br> showmessage('createproc err');<br> caption := 'err';<br> Exit;<br> End;<br> CloseHandle(hWritePipe);<br> ret := false;<br> FillChar(sBuffer, Sizeof(sBuffer), #0);<br> repeat<br> ret := ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, nil);<br> ListBox1.Items.Add(sBuffer);<br> until<br> ret = false;<br> CloseHandle(proc.hProcess);<br> CloseHandle(proc.hThread);<br> CloseHandle(hReadPipe);<br> caption := sOutput;<br>end;<br><br><br>