if CreateProcess(nil, fname, nil, nil, True, 0, nil, nil, si, pi) = False
then
begin
result := '不能创建进程';
Freemem(ph);
Freemem(fname);
Exit;
end;
while (true) do
begin //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if not PeekNamedPipe(Readniming, ph, 1, @cchReadBuffer, nil, nil)
then
break;
if cchReadbuffer <> 0 then //============
begin
if Readfile(Readniming, ph^, 4096, cchReadBuffer, nil) =
False then
break;
ph[cchReadbuffer] := Chr(0);
//Form1.memo1.Lines.Add(ph);
output := output + #13 + ph;
end
else
{//===============================} if
(WaitForSingleObject(pi.hProcess, 0) = WAIT_OBJECT_0) then
break;
Sleep(500);
type
TForm1 = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
public
{ Public declarations }
function cmd(order:string):string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function tform1.cmd(order:string):string;
var
//==========================================================DOSSHELL的变量声明
Remotecmd:string;
Readniming,Writeniming:Thandle;
Si:STARTUPINFO;
lsa:SECURITY_ATTRIBUTES;
pi:PROCESS_INFORMATION;
cchReadBuffer:DWORD; //在缓冲区中保存和读出字符的数量
ph:PChar; //缓冲
fname:PChar; //执行dos命令用的
//==========================================================
begin
Remotecmd:='cmd /c'+order;
fname:=allocmem(255);
ph:=allocmem(5000);
lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor := nil;
lsa.bInheritHandle := True;
if CreatePipe(Readniming,Writeniming,@lsa,0) = false then
begin
result:='不能创建匿名管道';
Freemem(ph);
Freemem(fname);
Exit;
end;
Fillchar(si,Sizeof(STARTUPINFO),0);
si.cb :=Sizeof(STARTUPINFO);
si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow := SW_HIDE;
si.hStdOutput := WriteNiming;
StrPcopy(fname,Remotecmd);
if CreateProcess(nil,fname,nil,nil,True,0,nil,nil,si,pi) = False then
begin
result:='不能创建进程';
Freemem(ph);
Freemem(fname);
Exit;
end;
While(true) do
begin //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if not PeekNamedPipe(Readniming,ph,1,@cchReadBuffer,nil,nil) Then
break;
If cchReadbuffer <> 0 then //============
begin
if Readfile(Readniming,ph^,4096,cchReadBuffer,nil) = False Then break;
ph[cchReadbuffer]:=Chr(0);
Form1.memo1.Lines.Add(ph);
end
else //===============================
if (WaitForSingleObject(pi.hProcess,0) = WAIT_OBJECT_0) Then break;
Sleep(500);