bool WaitForExitCode(ProcessHandle handle, int* exit_code) { bool success = WaitForExitCodeWithTimeout(handle, exit_code, INFINITE); if(!success) { CloseProcessHandle(handle); } return success; }
bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, int64 timeout_milliseconds) { if(::WaitForSingleObject(handle, timeout_milliseconds) != WAIT_OBJECT_0) { return false; } DWORD temp_code; // Don't clobber out-parameters in case of failure. if(!::GetExitCodeProcess(handle, &temp_code)) { return false; } // Only close the handle on success, to give the caller a chance to forcefully // terminate the process if he wants to. CloseProcessHandle(handle); *exit_code = temp_code; return true; }
bool CServerSession::OnRunImage( CNcpMessage* pMsg, int nSize ) { char path[MAXPATH]; //parse the parameters bool bSync = (bool)pMsg->GetRet(); string strImageName = (char*)(pMsg->GetData()); if( !IsAbsDir( strImageName.c_str() ) ){ strcpy( path, m_strCurDir.c_str() ); CatDir( path, strImageName.c_str(), path, ELEMENTS(path) ); }else{ strcpy( path, strImageName.c_str() ); } BOOL bOk; int nPriority = m_pServApp->GetPriority(); if( bSync ){ //synchronize running, register the handle and wait the process HPROCESS hProcess; bOk = CreateProcess( path, m_strCurDir.c_str(), nPriority, &hProcess ); m_pServApp->RegistProc( hProcess ); WaitProcess( hProcess ); m_pServApp->UnregistProc( hProcess ); CloseProcessHandle( hProcess ); }else{ //just run the process and return. bOk = CreateProcess( path, m_strCurDir.c_str(), nPriority, NULL ); } //send back the return code. pMsg->Init( CM_ACK ); if( !bOk ){ pMsg->SetRet( E_NOEXEC ); } pMsg->Send( m_sock ); return bOk; }