hola companneros
tengo el siguiente codigo para crear un nuevo proceso de consola, donde si se ejecuta algo como "DIR" o "netstat" , pues esta muy bien y siempre funciona.
Ahora, necesito correr un comando que luego de correrlo se queda con un prompt esperando mas comandos de mi parte. Como esto lo hago desde dentro de mi programa, entro en un ciclo infinito.
Lo que necesito es poder controlar el nuevo proceso creado, que no afecte mi propio programa y lo mas importante que pueda seguir mandandole comandos hasta que decida cerrarlo, mandandole el comando adecuado.
muchas gracias de antemano
Código Delphi
[-]
Procedure TFormMain.EjecutaComando(C:String);
var
buf: array[0..4095] of byte;
si: STARTUPINFO;
sa: SECURITY_ATTRIBUTES;
sd: SECURITY_DESCRIPTOR;
pi: PROCESS_INFORMATION;
newstdin,newstdout,read_stdout,write_stdin: THandle;
exitcod,bread,avail: Cardinal;
count:integer;
begin
count:= 0 ;
if IsWinNT 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 CreatePipe(newstdin,write_stdin,@sa,0) then
begin
if CreatePipe(read_stdout,newstdout,@sa,0) then
begin
GetStartupInfo(si);
with si do
begin
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_HIDE;
hStdOutput := newstdout;
hStdError := newstdout;
hStdInput := newstdin;
end;
fillchar(buf,sizeof(buf),0);
GetEnvironmentVariable('COMSPEC',@buf,sizeof(buf)-1);
strcat(@buf,' /c ');
strcat(@buf,PAnsiChar(C));
if CreateProcess(nil,@buf,nil,nil,TRUE,CREATE_NEW_CONSOLE,nil,nil,si,pi) then
begin
memo1.lines.Clear;
GetExitCodeProcess(pi.hProcess,exitcod);
PeekNamedPipe(read_stdout,@buf,sizeof(buf)-1,@bread,@avail,nil);
while (exitcod = STILL_ACTIVE) or (bread > 0) do
begin
if (bread > 0) then
begin
fillchar(buf,sizeof(buf),0);
if (avail > sizeof(buf)-1) then
while (bread >= sizeof(buf)-1) do
begin
ReadFile(read_stdout,buf,sizeof(buf)-1,bread,nil);
memo1.lines.text := memo1.lines.Text + StrPas(@buf);
fillchar(buf,sizeof(buf),0);
end else
begin
ReadFile(read_stdout,buf,sizeof(buf)-1,bread,nil);
memo1.lines.text := memo1.lines.Text + StrPas(@buf);
end;
end;
GetExitCodeProcess(pi.hProcess,exitcod);
PeekNamedPipe(read_stdout,@buf,sizeof(buf)-1,@bread,@avail,nil);
end;
end;
CloseHandle(read_stdout);
CloseHandle(newstdout);
end;
CloseHandle(newstdin);
CloseHandle(write_stdin);
end;
end;