2012년 3월 9일 금요일

원도 프로그램에서 Consol Process Output캡춰하기

bool ExecuteNonConsolProcess(std::string sExeName, std::string sArguments) {
std::string sExecute = sExeName;
sExecute += " ";
sExecute += sArguments;

HANDLE hReadPipe, hWritePipe;

SECURITY_ATTRIBUTES securityAttr;
ZeroMemory( & securityAttr, sizeof(securityAttr));
securityAttr.nLength = sizeof(securityAttr);
securityAttr.bInheritHandle = TRUE;

CreatePipe( & hReadPipe, & hWritePipe, & securityAttr, 0);

STARTUPINFO sStartupInfo;
PROCESS_INFORMATION process_Info;
ZeroMemory( & sStartupInfo, sizeof(sStartupInfo));
ZeroMemory( & process_Info, sizeof(process_Info));

sStartupInfo.cb = sizeof(STARTUPINFO);
sStartupInfo.dwFlags = STARTF_USESTDHANDLES;
sStartupInfo.hStdInput = NULL;
sStartupInfo.hStdOutput = hWritePipe;
sStartupInfo.hStdError = hWritePipe;

BOOL bSuccessProcess = CreateProcess(0, (char * ) sExecute.c_str(), 0, 0, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
0, 0, & sStartupInfo, & process_Info);
CloseHandle(hWritePipe);

if (FALSE == bSuccessProcess) {
CloseHandle(hReadPipe);
return false;
}

char szTemp[260];
DWORD dwReadByte;
std::string sOutputString;
sOutputString.clear();

BOOL bResult;
do {
ZeroMemory(szTemp, sizeof(szTemp));
bResult = ::ReadFile(hReadPipe, szTemp, sizeof(szTemp) - 1, & dwReadByte, 0);

if (TRUE == bResult) {
TRACE("%s", szTemp);

sOutputString += szTemp;
}
} while (TRUE == bResult);

CloseHandle(hReadPipe);

return true;
}

댓글 없음:

댓글 쓰기