Capturar la salida de la consola - convertir ese code a c
Hola que tal, escribo este mensaje, para preguntar sobre uno de los trucos de delphi:
"capturar la salida de la consola"
lo que he estado intentado es convertirlo a c, pues no ha sido muy dificil, pero no logro hacerlo....
le digo que retorne result, pero solo me retorna el comando con todo organizado:
eje:
Cita:
C:\WINDOWS\system32\cmd.exe /c dir C:\
|
solo me retorna eso, cual sera el problema ?
en el codigo de delphi debajo de esta linea:
Cita:
StrCat(@Buffer,PChar(' /c ' + Cmd))
|
hago un showmessage y muestra eso mismo... tambien le pongo un cout a mi code y muestra eso tambien, osea que hasta ahi va bien.....
pero despues de esta linea del codigo de delphi:
Cita:
Result:= Result + String(PChar(@Buffer));
|
muestro un mensaje con la variable result y ahi ya me manda el comando ejecutado..
pero hago lo mismo con el mio y sigue dandome:
Cita:
C:\WINDOWS\system32\cmd.exe /c dir C:\
|
aqui pongo mi code:
Cita:
#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
using std::string;
using namespace std;
string CmdExec(string cmd);
int main(){
//ese main no improta mucho como vemos lo escribo en un txt y lo muestro en un mensaje tambien....
FILE *pfile;
string final;
pfile = fopen("C:\\dos.txt","wb");
final = CmdExec("dir C:\\");
fputs(final.c_str(),pfile);
MessageBox(NULL,final.c_str(),"pepp",MB_OK);
return 0;
}
string CmdExec(string cmd){
char s2[8] = " /c ";
char Buffer[4096];
string result;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
PROCESS_INFORMATION pi;
HANDLE newstdin, newstdout, read_stdout, write_stdin;
unsigned long bread, exitcod, avail;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, 0, false);
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
if(CreatePipe(&newstdin,&write_stdin,&sa,0)){
if(CreatePipe(&read_stdout,&newstdout,&sa,0)){
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES || STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = newstdout;
si.hStdError = newstdout;
si.hStdInput = newstdin;
memset(Buffer,sizeof(Buffer),0);
GetEnvironmentVariable("COMSPEC",Buffer,sizeof(Buffer) -1);
strcat(Buffer,s2);
strcat(Buffer,cmd.c_str());
if(CreateProcess(NULL,Buffer,NULL,NULL,true,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)){
do{
PeekNamedPipe(&read_stdout,&Buffer,sizeof(&Buffer) - 1,&bread,&avail,NULL);
if(bread > 0)
{
memset(Buffer,sizeof(Buffer),0);
ReadFile(&read_stdout,Buffer,bread,&bread,NULL);
result = result + Buffer;
cout << Buffer << endl;
}
GetExitCodeProcess(pi.hProcess,&exitcod);
}while((exitcod != STILL_ACTIVE) && (bread = 0));
CloseHandle(read_stdout);
CloseHandle(newstdout);
}
CloseHandle(write_stdin);
CloseHandle(newstdin);
}
}
return Buffer;
}
|
====================================
aqui pongo el code del truco de delphi:
Cita:
{ function IsWinNT: boolean;
var
OSV: OSVERSIONINFO;
begin
OSV.dwOSVersionInfoSize := sizeof(osv);
GetVersionEx(OSV);
result := OSV.dwPlatformId = VER_PLATFORM_WIN32_NT;
end; }
function CmdExec(Cmd: string): string;
var
Buffer: array[0..4096] of Char;
si: STARTUPINFO;
sa: SECURITY_ATTRIBUTES;
sd: SECURITY_DESCRIPTOR;
pi: PROCESS_INFORMATION;
newstdin, newstdout, read_stdout, write_stdin: THandle;
exitcod, bread, avail: Cardinal;
begin
Result:= '';
{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(Buffer, SizeOf(Buffer), 0);
GetEnvironmentVariable('COMSPEC', @Buffer, SizeOf(Buffer) - 1);
StrCat(@Buffer,PChar(' /c ' + Cmd));
result := String(PChar(@Buffer));
ShowMessage(result);
if CreateProcess(nil, @Buffer, nil, nil, TRUE, CREATE_NEW_CONSOLE, nil, nil, si, pi) then
begin
repeat
PeekNamedPipe(read_stdout, @Buffer, SizeOf(Buffer) - 1, @bread, @avail, nil);
if bread > 0 then
begin
Fillchar(Buffer, SizeOf(Buffer), 0);
ReadFile(read_stdout, Buffer, bread, bread, nil);
Result:= Result + String(PChar(@Buffer));
ShowMessage(result);
end;
Application.ProcessMessages;
GetExitCodeProcess(pi.hProcess, exitcod);
until (exitcod <> STILL_ACTIVE) and (bread = 0);
end;
CloseHandle(read_stdout);
CloseHandle(newstdout);
end;
CloseHandle(newstdin);
CloseHandle(write_stdin);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(CmdExec('dir C:\'));
end;
end.
|
Puse como comentario la funcion iswinnt y su uso en la funcion CmdExec, pero igual funciona para mostrar la salida en mi os..
ojala entiendan mi problema y puedan ayduarme gracias
Última edición por Stevenmn fecha: 24-02-2008 a las 16:55:53.
|