Procedure Cmdexecandview (filename:string; memo:tmemo); Procedure _addinfo (Mminfo:tmemo; s:string; var line:string); Var I, P:integer; Begin If MmInfo.Lines.Count > Then MmInfo.Lines.Clear; Remove \ R For I: = 0 to Length (S)-1 do If s[i] = #13 then s[i]: = '; Line: = line + S; \ n P: = Pos (#10, line); If p > 0 Then Begin Add a line to the front and leave the back to the next MMINFO.LINES.ADD (Copy (line, 1, p-1)); Line: = Copy (line, p + 1, Length (line)-p); End End Var Hreadpipe, Hwritepipe:thandle; Si:startupinfo; Lsa:security_attributes; Pi:process_information; Cchreadbuffer:dword; Ph:pchar; Fname:pchar; line:string; Begin FName: = AllocMem (1024); Ph: = ALLOCMEM (1024); Lsa.nlength: = sizeof (security_attributes); Lsa.lpsecuritydescriptor: = nil; Lsa.binherithandle: = True; If CreatePipe (Hreadpipe, Hwritepipe, @lsa, 0) = False Then Exit; Fillchar (SI, sizeof (startupinfo), 0); SI.CB: = sizeof (STARTUPINFO); Si.dwflags: = (startf_usestdhandles or Startf_useshowwindow); Si.wshowwindow: = Sw_hide; Si.hstdoutput: = Hwritepipe; Si.hstderror: = Hwritepipe; Strpcopy (fname, FileName); If CreateProcess (nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False Then Begin FREEMEM (ph); Freemem (fname); Exit; End CloseHandle (Hwritepipe); while (true) does Begin If not peeknamedpipe (hreadpipe, ph, 1, @cchReadBuffer, nil, nil) then break; If Cchreadbuffer <> 0 Then Begin If ReadFile (Hreadpipe, ph^, cchreadbuffer, nil) = False Then break; Ph[cchreadbuffer]: = Chr (0); _addinfo (memo, ph, line); End else if (WaitForSingleObject (pi.hprocess, 0) = WAIT_OBJECT_0) then break; Application.processmessages; Sleep (200); End Ph[cchreadbuffer]: = Chr (0); _addinfo (memo, ph, line); CloseHandle (Hreadpipe); CloseHandle (Pi.hthread); CloseHandle (pi.hprocess); FREEMEM (ph); Freemem (fname); End ———————————————— Call method (for example): Cmdexecandview (' ping 202.96.134.134 ', Memo1); There is a problem: if it is Cmdexecandview (' ping 202.96.134.134-t ', Memo1); Execution will continue to output, no way to stop, click the button to close the window did not respond, can only end the task, look forward to solve the problem. |