Tutaj masz funkcję z
Delphi
- wiem, że inny język, ale to miałem pod ręką, a i tak będziesz musiała skorzystać praktycznie w tych samych funkcji:
//Parametry: Executable - twój plik *.exe, CommandLine - dodatkowe parametry programu, Directory - katalog uruchomieniowy
function ExecuteConsole(Executable:PChar; CommandLine:PChar; Directory:PChar):boolean;
const bufSize = 256;
var readPipe :THandle;
writePipe :THandle;
security :SECURITY_ATTRIBUTES;
info :STARTUPINFO;
process :PROCESS_INFORMATION;
buf :array[0..bufSize-1] of char;
bytesRead :DWord;
text :string;
line :string;
newLinePos :integer;
begin
result:=FALSE;
security.nLength:=sizeof(security);
security.lpSecurityDescriptor:=nil;
security.bInheritHandle:=TRUE;
if CreatePipe(readPipe, writePipe, @security, 0) then
begin
ZeroMemory(@info, sizeof(info));
with info do
begin
cb := sizeof( info );
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := 0;
hStdOutput := writePipe;
hStdError := writePipe;
end;
if CreateProcess(Executable, CommandLine, nil, nil, TRUE, 0, nil, Directory, info, process) then
begin
CloseHandle( writePipe );
text:='';
while ReadFile( readPipe, buf, bufSize, bytesRead, nil) do
begin
text:=text+buf;
repeat
newLinePos:=Pos(#13, text);
if (newLinePos=0) then break;
line:=copy(text,1, newLinePos-1);
delete(text, 1, newLinePos);
//'line' zawiera tu już pełną linię tekstu z wyjścia uruchomionego programu
Form1.Memo1.Lines.Add(line);
until Application.Terminated;
ZeroMemory(@buf, bufSize);
end;
result:=TRUE;
end;
CloseHandle( readPipe );
end;
end;