Текущий архив: 2004.02.02;
Скачать: CL | DM;
Вниз
перенаправление вывода ? Найти похожие ветки
← →
atmospheric (2004-01-20 15:09) [0]как принять вывод консольной(дос) программы в своей программе ?
← →
Яичница © (2004-01-20 15:55) [1]
procedure ExecProg;
var
ProcInfo: TProcessInformation;
StartupInfo: TStartupInfo;
SecAttr: TSecurityAttributes;
SecDesc:TSecurityDescriptor;
newstdout,read_stdout:THandle; //дескрипторы пайпов
ExitCode:DWORD; //код завершения процесса
bread:DWORD; //кол-во прочитанных байт
avail:DWORD; //кол-во доступных байт
Buf:array[0..1023]of char;
begin
Screen.Cursor:=crHourGlass;
Form1.StatusBar1.Panels[0].Text:=Command;
Form1.Memo1.Clear;
ExitCode:=0;
BRead:=0; //кол-во прочитанных байт
avail:=0; //кол-во доступных байт
ZeroMemory(@buf,sizeof(Buf));
InitializeSecurityDescriptor(@SecDesc,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SecDesc, true, 0, false);
SecAttr.lpSecurityDescriptor :=@SecDesc;
SecAttr.nLength := sizeof(SECURITY_ATTRIBUTES);
SecAttr.bInheritHandle := true; //разрешаем наследование дескрипторов
if not(CreatePipe(read_stdout,newstdout,@SecAttr,0)) then//создаем пайп для stdout
begin
MessageBox(Form1.Handle,"CreatePipe",0,MB_OK+MB_ICONERROR);
CloseHandle(newstdout);
CloseHandle(read_stdout);
Screen.Cursor:=crDefault;
exit;
end;
GetStartupInfo(StartupInfo); //создаем startupinfo для дочернего процесса
// Параметр dwFlags сообщает функции CreateProcess
// как именно надо создать процесс.
// STARTF_USESTDHANDLES управляет полями hStd*.
// STARTF_USESHOWWINDOW управляет полем wShowWindow.
StartupInfo.dwFlags := STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdOutput := newstdout;
StartupInfo.hStdError := newstdout; //подменяем дескрипторы для
//создаем дочерний процесс
if not(CreateProcess(0,pchar(Command),0,0,TRUE,CREATE_NEW_CONSOLE,
0,0,StartupInfo,ProcInfo)) then
begin
Windows.MessageBox(Form1.Handle,"CreateProcess",0,MB_OK+MB_ICONERROR);
CloseHandle(newstdout);
CloseHandle(read_stdout);
Screen.Cursor:=crDefault;
exit;
end;
while(true)do //основной цикл программы
begin
GetExitCodeProcess(ProcInfo.hProcess,ExitCode); //пока дочерний процесс не закрыт
if (ExitCode <> STILL_ACTIVE)then break;
end;
PeekNamedPipe(read_stdout,@buf,1023,@bread,@avail,0);
//Проверяем, есть ли данные для чтения в stdout
if (bread <> 0) then
begin
ZeroMemory(@buf,sizeof(Buf));
if (avail > 1023)then
begin
while (bread >= 1023)do
begin
ReadFile(read_stdout,buf,1023,bread,0); //читаем из пайпа stdout
Form1.Memo1.Lines.Add(pchar(@buf));
ZeroMemory(@buf,sizeof(Buf));
end;
end
else
begin
ReadFile(read_stdout,buf,1023,bread,0);
Form1.Memo1.Lines.Add(pchar(@buf));
end
end;
CloseHandle(ProcInfo.hThread);
CloseHandle(ProcInfo.hProcess);
CloseHandle(newstdout);
CloseHandle(read_stdout);
Screen.Cursor:=crDefault;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Command:="Путь\Прога";
ExecProg;
end;
Страницы: 1 вся ветка
Текущий архив: 2004.02.02;
Скачать: CL | DM;
Память: 0.47 MB
Время: 0.016 c