| 发表于:2007-04-18 14:47:505楼 得分:2 |
用命名管道 俺写的 procedure rundoscommand(command: string; output: tstringlist; timeout: integer); var hreadpipe: thandle; hwritepipe: thandle; si: tstartupinfo; pi: tprocessinformation; sa: tsecurityattributes; // sd : tsecuritydescriptor; bytesread: dword; dest: array[0..1023] of char; cmdline: array[0..512] of char; tmplist: tstringlist; avail, exitcode, wrresult: dword; osver: tosversioninfo; tmpstr: ansistring; all, send: string; tc: integer; begin begin // initializesecuritydescriptor(@sd, security_descriptor_revision); // setsecuritydescriptordacl(@sd, true, nil, false); sa.nlength := sizeof(sa); sa.lpsecuritydescriptor := nil; //@sd; sa.binherithandle := true; createpipe(hreadpipe, hwritepipe, @sa, 0); end; try fillchar(si, sizeof(si), 0); si.cb := sizeof(tstartupinfo); si.wshowwindow := sw_hide; si.dwflags := startf_useshowwindow; si.dwflags := si.dwflags or startf_usestdhandles; si.hstdoutput := hwritepipe; si.hstderror := hwritepipe; strpcopy(cmdline, 'cmd.exe /c ' + command); tc := 0; if createprocess(nil, cmdline, nil, nil, true, normal_priority_class, nil, nil, si, pi) then begin exitcode := 0; while exitcode = 0 do begin wrresult := waitforsingleobject(pi.hprocess, 500); inc(tc, 500); bytesread := 1024; while bytesread > 0 do if peeknamedpipe(hreadpipe, @dest[0], 1024, @avail, nil, nil) then begin if avail > 0 then begin try fillchar(dest, sizeof(dest), 0); readfile(hreadpipe, dest[0], avail, bytesread, nil); tmpstr := copy(dest, 0, bytesread); all := all + tmpstr; finally end; end else break; end else break; if wrresult <> wait_timeout then begin exitcode := 1; // output.add( 'ôëððíê³é£¡ ') end else begin if tc > timeout then if terminateprocess(pi.hprocess, 0) then begin send := 'ôëðð³¬ê±£¬ç¿öæ½áêø£¡ ' end else begin send := 'ôëðð³¬ê±£¬ç¿öæ½áêøê§°ü£¡ ' end; end; end; getexitcodeprocess(pi.hprocess, exitcode); closehandle(pi.hprocess); closehandle(pi.hthread); end else output.add( 'ôëðð²»äü½øð𣡠'); output.text := all; if send <> ' ' then output.add(send); all := ' '; tmpstr := ' '; send := ' '; finally closehandle(hreadpipe); closehandle(hwritepipe); end; end; | | |
|