procedure Test;
var buf:array[0..1023]of byte;
si:STARTUPINFO;
sa:SECURITY_ATTRIBUTES;
sd:SECURITY_DESCRIPTOR;
pi:PROCESS_INFORMATION;
newstdin,newstdout,read_stdout,write_stdin:THandle;
exit_:cardinal;
bread:cardinal;
avail:cardinal;
tmp:integer;s:string;
begin
if Win32Platform=VER_PLATFORM_WIN32_NT then begin
InitializeSecurityDescriptor(@sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@sd,true,nil,false);
sa.lpSecurityDescriptor:=@sd;
end else sa.lpSecurityDescriptor:=nil;
sa.nLength:=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle:=true;
if not CreatePipe(newstdin,write_stdin,@sa,0)then exit;
if not CreatePipe(read_stdout,newstdout,@sa,0)then begin
CloseHandle(newstdin);CloseHandle(write_stdin);
exit;
end;
GetStartupInfo(si);
si.dwFlags:=STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
si.wShowWindow:=SW_HIDE;
si.hStdOutput:=newstdout;
si.hStdError:=newstdout;
si.hStdInput:=newstdin;
if not CreateProcess('c:\\winnt\\system32\\cmd.exe',nil,nil,nil,true,
CREATE_NEW_CONSOLE,nil,nil,si,pi)then begin
CloseHandle(newstdin);CloseHandle(newstdout);
CloseHandle(read_stdout);CloseHandle(write_stdin);
exit;
end;
ReadFile(read_stdout,buf,1023,bread,nil);
for tmp:=0to 214738do begin
GetExitCodeProcess(pi.hProcess,exit_);
if not (exit_=STILL_ACTIVE) then break;
PeekNamedPipe(read_stdout,@buf[0],1023,@bread,@avail,nil);
if (bread<>0) then begin
ZeroMemory(@buf[0],length(buf));
if (avail > 1023) then begin
while (bread >= 1023) do begin
ReadFile(read_stdout,buf,1023,bread,nil);
Form1.Memo1.Text:=Form1.Memo1.Text+Strpas(@buf[0]);
ZeroMemory(@buf[0],length(buf));
end;
end else begin
ReadFile(read_stdout,buf,1023,bread,nil);
Form1.Memo1.Text:=Form1.Memo1.Text+StrPas(@buf[0]);
ZeroMemory(@buf[0],length(buf));
end;
end;
s:=Form1.Label1.Caption;
if s<>''then begin
WriteFile(write_stdin,s[1],length(s),bread,nil);
Form1.Label1.Caption:='';
end;
Application.ProcessMessages;
end;
TerminateProcess(pi.hProcess,0);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(newstdin); //clean stuff up
CloseHandle(newstdout);
CloseHandle(read_stdout);
CloseHandle(write_stdin);
end;