티스토리 뷰
Programming/MFC(C++)
[MFC] CreateProcess와 CreatePipe를 이용하여 ping command 실행 하고 결과 읽기
Zadd 2018. 10. 26. 17:05Visual Studio 2012
MFC에서 ping command 실행하여 값을 확인해보자.
1. 함수원형은 다음과 같다.
매개변수인 pCmdArg에 기존 명령프롬프트에서 사용하는 명령어를 넣으면 된다.
// ... CString ExecCmd( LPCSTR pCmdArg); //...
2. 소스코드
클래스이름에 본인이 작업할 클래스명을 적으면 된다.
Pipe를 통해서 CreateProcess로 만들어진 자식프로세스의 읽기나 쓰기가 가능하다
// ...
CString 클래스이름::ExecCmd( LPCSTR pCmdArg)
{
// Handle Inheritance - to pipe child's stdout via pipes to parent, handles must be inherited.
// SECURITY_ATTRIBUTES.bInheritHandle must be TRUE
// CreateProcess parameter bInheritHandles must be TRUE;
// STARTUPINFO.dwFlags must have STARTF_USESTDHANDLES set.
CString strResult; // Contains result of cmdArg.
HANDLE hChildStdoutRd; // Read-side, used in calls to ReadFile() to get child's stdout output.
HANDLE hChildStdoutWr; // Write-side, given to child process using si struct.
BOOL fSuccess;
// Create security attributes to create pipe.
SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES)} ;
saAttr.bInheritHandle = TRUE; // Set the bInheritHandle flag so pipe handles are inherited by child process. Required.
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe to get results from child's stdout.
// I'll create only 1 because I don't need to pipe to the child's stdin.
if ( !CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0) )
{
return strResult;
}
STARTUPINFO si = { sizeof(STARTUPINFO) }; // specifies startup parameters for child process.
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; // STARTF_USESTDHANDLES is Required.
si.hStdOutput = hChildStdoutWr; // Requires STARTF_USESTDHANDLES in dwFlags.
si.hStdError = hChildStdoutWr; // Requires STARTF_USESTDHANDLES in dwFlags.
// si.hStdInput remains null.
si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing. Requires STARTF_USESHOWWINDOW in dwFlags.
PROCESS_INFORMATION pi = { 0 };
// Create the child process.
fSuccess = CreateProcess(
NULL,
(LPSTR)pCmdArg, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // TRUE=handles are inherited. Required.
CREATE_NEW_CONSOLE, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&si, // __in, STARTUPINFO pointer
&pi); // __out, receives PROCESS_INFORMATION
if (! fSuccess)
{
return strResult;
}
// Wait until child processes exit. Don't wait forever.
WaitForSingleObject( pi.hProcess, 2000 );
TerminateProcess( pi.hProcess, 0 ); // Kill process if it is still running. Tested using cmd "ping blah -n 99"
// Close the write end of the pipe before reading from the read end of the pipe.
if (!CloseHandle(hChildStdoutWr))
{
return strResult;
}
// Read output from the child process.
for (;;)
{
DWORD dwRead;
CHAR chBuf[4096];
// Read from pipe that is the standard output for child process.
bool done = !ReadFile( hChildStdoutRd, chBuf, 4096, &dwRead, NULL) || dwRead == 0;
if( done )
{
break;
}
// Append result to string.
strResult += CString( chBuf, dwRead) ;
}
// Close process and thread handles.
CloseHandle( hChildStdoutRd );
// CreateProcess docs specify that these must be closed.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return strResult;
}
//...
3. 테스트 하기
자기 자신을 핑검색 한 후에 검색 내용을 메시지박스로 표현한다.
코드 실행은 다음과 같이 진행한다.
// ...
CString strResult="";
strResult = ExecCmd("ping 127.0.0.1");
AfxMessageBox(strResult);
//...
끝
'Programming > MFC(C++)' 카테고리의 다른 글
| [MFC] 다이얼로그 크기 조절 시 컨트롤 사이즈 자동 조절하기(EasySize) (0) | 2018.10.30 |
|---|---|
| [MFC] 운영체제(OS) Bit 확인하기 (0) | 2018.10.26 |
| [MFC] CListCtrl 클립보드 복사 구현하기(Ctrl+C 기능 구현) (1) | 2018.10.26 |
| [MFC] 람다 표현으로 CArray 정렬하기 (0) | 2018.10.22 |
| [MFC] 멀티스레드 사용하여 소켓통신 채팅 서버 프로그램 만들기 (9) | 2018.07.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- SendMessage
- MXCOMPONENT
- ADODB
- 모달리스
- #자동업데이트
- 스티커메모로드중
- MFC
- 청년내일채움공제
- 모달리스 다이얼로그
- 프로세스이름변경
- 스쿠트항공 환불받기
- MFC ADO
- CMFCBUTTON
- 전자신고변환
- 세금계산서합계표양식
- MFC Modeless
- 항공알파벳
- 부가가치세
- 청년내일채움공제 만기신청
- 해외송금확인
- Modeless
- 법정동코드
- sqlite3
- MSSQL
- 전자신고파일설명서
- 부가가치세전산매체
- Sticky Notes Loading
- 소켓
- c++
- ADO
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함