Estaba echándole otro vistazo a este hilo, y si lo que se quería al principio era recoger la salida de un programa, se puede hacer sin usar el interprete de comandos para que redirija su salida a un archivo y luego leer ese archivo.
Aquí os dejo un ejemplo de cómo recoger la salida del comando dir y colocarla en un Tmemo. En el formulario debe de haber un boton y un memo llamado memo1.
Código Delphi
[-]
function IsWinNT: boolean;
var osv: OSVERSIONINFO;
begin
osv.dwOSVersionInfoSize := sizeof(osv);
GetVersionEx(osv);
result:= osv.dwPlatformId = VER_PLATFORM_WIN32_NT;
end;
procedure TForm1.Button1Click(Sender: TObject);
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;
begin
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 dir');
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;
Hasta luego