Red de conocimiento del abogados - Respuesta jurídica de la empresa - Delphi llama a DOS y genera el conjunto de resultados

Delphi llama a DOS y genera el conjunto de resultados

unidad Unidad1;

interfaz

usos

Windows, Mensajes, SysUtils, Variantes, Clases, Gráficos, Controles, Formularios,

Diálogos, StdCtrls;

tipo

TForm1 = clase(TForm)

Memo1:TMemo;

procedimiento FormClose(Remitente:TObject; var Acción:TCloseAction);

procedimiento FormCreate(Remitente:TObject);

procedimiento ress(Remitente:TObject; var Clave:Char);

privado

{ Declaraciones privadas }

público

{ Declaraciones públicas }

fin;

var

Form1 :TForm1;

Lectura, Escritura :THandle;

Lectura, Escritura :THandle;

InfoProceso :TProcessInformation;

implementación

{$R *.dfm}

procedimiento InitConsole; {Establecer atributos de inicio de la consola}

var

Secu :TSecurityAttributes;

start :TStartUpInfo;

buf :array[0..MAX_PATH] of Char;

begin // MAX_PATH

con Secu do

comenzar

nlength := SizeOf(TSecurityAttributes);

binherithandle := true;

lpsecuritydescriptor := nil;

end;

Createpipe(ReadOut, WriteOut, @Secu, 0); {Crear una canalización con nombre para capturar la salida del programa de la consola}

Createpipe(ReadIn, WriteIn, @Secu, 0); {Crear una segunda canalización con nombre para capturar la entrada del programa de la consola}

GetEnvironmentVariable('comspec', buf, SizeOf(buf) ) ;

MemoriaCero(@start, SizeOf(start));

start.cb := SizeOf(start);

start.hStdOutput := WriteOut

start.hStdInput := LeerEn;

start.hStdError := WriteOut;

start.dwFlags := STARTF_USESTDHANDLES +

STARTF_USESHOWWINDOW;

start.wShowWindow := SW_HIDE;

CreateProcess(nil, buf,

@Secu, @Secu, true,

NORMAL_PRIORITY_CLASS,

nil, nil, start, ProcessInfo);

end;

función ReadFromPipe:string;

var

buf :array[0..1024] of Char;

var

p>

BytesRead :DWord;

comenzar

Resultado := '';

while

BytesRead > 0 do

begin //Bucle para leer datos.

si ReadFile(ReadOut, buf, SizeOf(buf), BytesRead, nil) entonces

comenzar

Resultado:= Resultado + Copiar(buf, 1, BytesRead);

WaitForSingleObject(ProcessInfo.hProcess, 10);

// Form1.Memo1.Lines.Add('a ' + inttostr(BytesRead));

end

else

break;

PeekNamedPipe(ReadOut, nil, 0, nil, @BytesRead, nil); Hay datos.

// Form1.Memo1.Lines.Add('b ' + inttostr(BytesRead));

end;

end;

procedimiento WriteCMD(Valor:cadena);

var

len :integer;

BytesWrite :DWord;

Buffer :PChar ;

comenzar

len := Longitud(Valor) + 2;

Buffer := PChar(Valor + #13#10); //Comando y El contenido no aparece en una sola línea.

WriteFile(WriteIn, Buffer[0], len, BytesWrite, nil);

end;

procedimiento CloseConsole;

comenzar

TerminateProcess(ProcessInfo.hProcess, 0);

CloseHandle(ProcessInfo.hProcess);

CloseHandle(ProcessInfo.hThread);

CloseHandle(ReadIn);

CloseHandle(WriteIn);

CloseHandle(ReadOut);

CloseHandle(WriteOut);

fin ;

procedimiento TForm1.FormClose(Sender:TObject; var Action:TCloseAction);

comenzar

CerrarConsole;

fin;

procedimiento TForm1.FormCreate(Remitente:TObject);

comenzar

InitConsole;

finalizar;

procedimiento TForm1.ress(Remitente:TObject; var Key:Char);

var

s, ss: cadena;

i, j: entero;

comenzar

si clave = #13 entonces

comenzar

i := Memo1.Lines.Count - 1;

s := Memo1.Lines[i];

eliminar(s, 1, pos('>', s));

WriteCMD(TRIM(s));

//WaitForSingleObject(ProcessInfo.hProcess, 10000);//WAIT_TIMEOUT);

sleep(1);

// ss := TRIM(ReadFromPipe );

// for j = (Memo1.Lines.Count-1) downto 0 do

// comenzar

// if Memo1.Lines[ j] = '' luego Memo1.Lines.delete(j);

// fin;

Memo1.Lines.Add(TRIM(ReadFromPipe));

fin;

fin;

fin.