参见下面的代码
procedure TMainForm.Button2Click(Sender: TObject);
var
OutHandle:THandle;
sinfo:_STARTUPINFOA;
processinfo:_PROCESS_INFORMATION;
CurFileName:string;
ExitCode:Cardinal;
nCount:integer;
begin
CurFileName:=CurDir+'/temp.txt';
//临时文件名
//create the output file 's handle
OutHandle:=CreateFile(pchar(CurFileName),GENERIC_WRITE,FILE_SHARE_WRITE,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
//set the start information property
sinfo.cb:=sizeof(sinfo);
sinfo.lpReserved:=nil; //set all reserved property to the defined property
sinfo.lpDesktop:=nil;
sinfo.lpTitle:=nil;
sinfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES ;
sinfo.wShowWindow:=SW_HIDE;
sinfo.cbReserved2:=0;
sinfo.lpReserved2:=nil;
sinfo.hStdOutput:=OutHandle;//set the output handle
sinfo.hStdInput:=STD_INPUT_HANDLE ;//set to default
sinfo.hStdError:=STD_ERROR_HANDLE ;
if not(CreateProcess(nil,pchar(Edit1.Text),nil,nil,true,CREATE_NEW_CONSOLE,nil,nil,sinfo,processinfo))
then
begin
ShowMessage('Process can not be created');
CloseHandle(OutHandle);
end;
nCount:=0;
//set the time out
//wait for process stop
while (GetExitCodeProcess(processinfo.hProcess,ExitCode))and(ExitCode=STILL_ACTIVE)and(nCount<MaxCount) do
begin
Sleep(500);
inc(nCount);
end;
if not(GetExitCodeProcess(processinfo.hProcess,ExitCode)) or (ExitCode=STILL_ACTIVE)
then //time out , mannualy close process
TerminateProcess(processinfo.hProcess,0);
CloseHandle(OutHandle);
//load the output to the memo
memo1.Lines.LoadFromFile(CurFileName);
end;
用相同的方法就可以解决你的问题,具体的文章请看这里:
http://go7.163.com/zeroworld/program/process.html
在该文中使用了文件流,可以直接使用内存流,那样就更好了。