使用CreateProcess,下面这段程序开启Dos程序,将输出放到temp.txt文件<br>,并从文件中读入到Memo中。<br><br>要下在示例程序,到http://go7.163.com/zeroworld/program/process.html来看看<br><br>var<br> OutHandle:THandle;<br> sinfo:_STARTUPINFOA;<br> processinfo:_PROCESS_INFORMATION;<br> CurFileName:string;<br> ExitCode:Cardinal;<br> nCount:integer;<br>begin<br> CurFileName:=CurDir+'/temp.txt';<br> //临时文件名<br><br><br> //create the output file 's handle<br> OutHandle:=CreateFile(pchar(CurFileName),GENERIC_WRITE,FILE_SHARE_WRITE,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);<br><br> //set the start information property<br> sinfo.cb:=sizeof(sinfo);<br> sinfo.lpReserved:=nil; //set all reserved property to the defined property<br> sinfo.lpDesktop:=nil;<br> sinfo.lpTitle:=nil;<br> sinfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES ;<br> sinfo.wShowWindow:=SW_HIDE;<br> sinfo.cbReserved2:=0;<br> sinfo.lpReserved2:=nil;<br> sinfo.hStdOutput:=OutHandle;//set the output handle<br> sinfo.hStdInput:=STD_INPUT_HANDLE ;//set to default<br> sinfo.hStdError:=STD_ERROR_HANDLE ;<br><br> if not(CreateProcess(nil,pchar(Edit1.Text),nil,nil,true,CREATE_NEW_CONSOLE,nil,nil,sinfo,processinfo))<br> then<br> begin<br> ShowMessage('Process can not be created');<br> CloseHandle(OutHandle);<br> end;<br><br> nCount:=0; <br> //set the time out<br><br> //wait for process stop<br> while (GetExitCodeProcess(processinfo.hProcess,ExitCode))and(ExitCode=STILL_ACTIVE)and(nCount<MaxCount) do<br> begin<br> Sleep(500);<br> inc(nCount);<br> end;<br><br> if not(GetExitCodeProcess(processinfo.hProcess,ExitCode)) or (ExitCode=STILL_ACTIVE)<br> then //time out , mannualy close process<br> TerminateProcess(processinfo.hProcess,0); <br><br> CloseHandle(OutHandle);<br><br> //load the output to the memo<br> memo1.Lines.LoadFromFile(CurFileName);<br><br><br>end; <br><br>