[델파이] 콘솔 어플리케이션의 표준출력/표준에러 받아오기
델파이2011. 12. 28. 17:13
[출처 : http://delphi.borlandforum.com/impboard/impboard.dll?action=read&db=del_tip&no=148 - 임프(박지훈) 님 글]
- procedure TForm1.Button1Click(Sender: TObject);
- var
- start: TStartupInfo;
- sec: TSecurityAttributes;
- pinfo: TProcessInformation;
- hwrite, hread: THandle;
- BytesRead: DWORD;
- Buffer: array[0..512] of char;
- ResultString: string;
- begin
- sec.nLength := sizeof(sec);
- sec.lpSecurityDescriptor := nil;
- sec.bInheritHandle := true;
- // 어노니머스 파이프 생성
- if CreatePipe(hread, hwrite, @sec, 0)<>true then
- begin
- ShowMessage('Fail to open pipe.');
- exit;
- end;
- // 콘솔어플리케이션 프로세스 실행을 위한 준비
- FillChar(start, sizeof(STARTUPINFO), 0);
- start.cb := sizeof(start);
- start.dwFlags := STARTF_USESTDHANDLES;
- start.hStdOutput := hwrite; // 표준출력(stdout) 리다이렉션
- start.hStdError := hwrite; // 표준에러(stderr) 리다이렉션
- // 콘솔어플리케이션 프로세스 실행
- if CreateProcess(nil, 'dcc32.exe', nil, nil, true, DETACHED_PROCESS, nil, nil, start, pinfo) <> true then
- begin
- ShowMessage(AnsiString('CreateProcess() failed: ') + IntToStr(GetLastError));
- exit;
- end;
- CloseHandle(hwrite);//이것을 하지 않으면 프로세스가 block된다
- while ReadFile(hread, Buffer, Length(buffer)-1, BytesRead, nil) and (BytesRead>0) do
- begin
- Buffer[BytesRead] := #0;
- ResultString := ResultString + Buffer;
- {if GetAsyncKeyState(VK_ESCAPE)<0 then // 실행에 많은 시간이 걸릴 경우
- begin // 주석해제 필요
- ShowMessage('Canceled.');
- break;
- end;
- Application.ProcessMessages;}
- end;
- CloseHandle(hread);
- Memo1.Lines.Text := ResultString;
- end;
'델파이' 카테고리의 다른 글
[델파이] 프록시(Proxy) 설정 (1) | 2010.12.24 |
---|---|
[델파이] SHDocVw_TLB (0) | 2010.12.21 |
[델파이] 예외 처리 클래스의 종류 (0) | 2009.11.13 |
[델파이] DLL Injection (1) | 2009.06.17 |
[델파이] 기본 함수 정리 (0) | 2009.06.15 |
델파이에서 메모리 누수 확인 (0) | 2009.04.20 |
IE 기본 툴바에 버튼 생성하기 (0) | 2009.03.10 |
현재 페이지의 모든 쿠키 읽어 오기 (0) | 2009.01.29 |
서버 상태 확인 (0) | 2009.01.12 |
BeforeNavigate2 이벤트에서 PostData 읽어오기 (GET 방식과 POST 방식의 구분) (0) | 2008.12.04 |