Delphi Execution DOS command displayed to text box

Source: Internet
Author: User

Delphi Execution DOS command displayed to text box (TMemo)

2007-11-24 22:03

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.

http://blog.csdn.net/shao9900/article/details/43951633

Delphi Execution DOS command displayed to text box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.