Procedure TForm1.Dos2Win(CmdLine : string; var Line : string); const BUFSIZE = 2000; var SecAttr : TSecurityAttributes; hReadPipe, hWritePipe : THandle; StartupInfo : TStartUpInfo; ProcessInfo : TProcessInformation; Buffer : Pchar; WaitReason, BytesRead : DWord; begin with SecAttr do begin nlength := SizeOf(TSecurityAttributes); bInheritHandle := true; lpSecurityDescriptor := nil; end; if CreatePipe (hReadPipe, hWritePipe, @SecAttr, 0) then begin Buffer := AllocMem(BUFSIZE + 1); FillChar(StartupInfo, Sizeof(StartupInfo), #0); StartupInfo.cb := SizeOf(StartupInfo); StartupInfo.hStdOutput := hWritePipe; StartupInfo.hStdInput := hReadPipe; StartupInfo.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(CmdLine), @SecAttr, @SecAttr, true, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then begin repeat WaitReason := WaitForSingleObject( ProcessInfo.hProcess,100); Application.ProcessMessages; until (WaitReason <> WAIT_TIMEOUT); repeat BytesRead:=0; ReadFile(hReadPipe, Buffer[0], BUFSIZE, BytesRead, nil); Buffer[BytesRead]:= #0; OemToAnsi(Buffer,Buffer); Line := Line + string(Buffer); until (BytesRead < BUFSIZE); end; FreeMem(Buffer); CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); CloseHandle(hReadPipe); CloseHandle(hWritePipe); end; end;
procedure TForm1.Button1Click(Sender: TObject); var OutLine : string; begin // ProgramPath - path to 'Prg.exe' program // Command - Командная строка // ShellExecute(0,'open',PChar('Prg.exe'),PChar(Command),PChar(ProgramPath),SW_HIDE); Dos2Win('Prg.exe',OutLine); // OutLine - Information Line! end;
Help!
Почему приведенный код не работает с программами? (В данном случае - 'Prg.exe') С *.bat файлами все в порядке. При их использовании OutLine содержит перехваченную информацию с консольного приложения(bat). Наоборот, при использовании файлов *.exe функция Dos2Win виснет , хотя программа запускается и выполняется отменно! Почему? Замена SW_HIDE на SW_SHOW не помогла. Мне кажется, что во всем виновата функция ReadFile() из функции Dos2Win(). Как лечить?
Сообщение отредактировано: volvo - 5.04.2010 17:08