From 0134e7d53e1c25cd5db4f8ca600be0b58abbabc7 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Fri, 26 Aug 2011 11:56:28 +0100 Subject: [PATCH] Implement ProcessUtils::runSync() under Windows This follows the existing async launching path and then uses WaitForSingleObject / GetExitCodeProcess to wait for the process and get its status. --- src/ProcessUtils.cpp | 46 +++++++++++++++++++++++++++++++------------- src/ProcessUtils.h | 12 ++++++++---- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/ProcessUtils.cpp b/src/ProcessUtils.cpp index ce5237a..e5e3f1f 100644 --- a/src/ProcessUtils.cpp +++ b/src/ProcessUtils.cpp @@ -37,7 +37,7 @@ int ProcessUtils::runSync(const std::string& executable, #ifdef PLATFORM_UNIX return runSyncUnix(executable,args); #else - return runSyncWindows(executable,args); + return runWindows(executable,args,RunSync); #endif } @@ -52,20 +52,11 @@ int ProcessUtils::runSyncUnix(const std::string& executable, } #endif -#ifdef PLATFORM_WINDOWS -int ProcessUtils::runSyncWindows(const std::string& executable, - const std::list& args) -{ - // TODO - Implement me - return 0; -} -#endif - void ProcessUtils::runAsync(const std::string& executable, const std::list& args) { #ifdef PLATFORM_WINDOWS - runAsyncWindows(executable,args); + runWindows(executable,args,RunAsync); #elif defined(PLATFORM_UNIX) runAsyncUnix(executable,args); #endif @@ -358,8 +349,9 @@ PLATFORM_PID ProcessUtils::runAsyncUnix(const std::string& executable, #endif #ifdef PLATFORM_WINDOWS -void ProcessUtils::runAsyncWindows(const std::string& executable, - const std::list& args) +int ProcessUtils::runWindows(const std::string& executable, + const std::list& args, + RunMode runMode) { std::string commandLine = executable; for (std::list::const_iterator iter = args.begin(); iter != args.end(); iter++) @@ -391,9 +383,37 @@ void ProcessUtils::runAsyncWindows(const std::string& executable, &startupInfo /* startup info */, &processInfo /* process information */ ); + if (!result) { LOG(Error,"Failed to start child process. " + executable + " Last error: " + intToStr(GetLastError())); + return -1; + } + else + { + if (runMode == RunSync) + { + if (WaitForSingleObject(processInfo.hProcess,INFINITE) == WAIT_OBJECT_0) + { + DWORD status = -1; + if (GetExitCodeProcess(processInfo.hProcess,&status) != 0) + { + LOG(Error,"Failed to get exit code for process"); + } + return status; + } + else + { + LOG(Error,"Failed to wait for process to finish"); + return -1; + } + } + else + { + // process is being run asynchronously - return zero as if it had + // succeeded + return 0; + } } } #endif diff --git a/src/ProcessUtils.h b/src/ProcessUtils.h index 6d76015..ce2b023 100644 --- a/src/ProcessUtils.h +++ b/src/ProcessUtils.h @@ -46,6 +46,11 @@ class ProcessUtils #endif private: + enum RunMode + { + RunSync, + RunAsync + }; static int runElevatedLinux(const std::string& executable, const std::list& args); static int runElevatedMac(const std::string& executable, @@ -55,11 +60,10 @@ class ProcessUtils static PLATFORM_PID runAsyncUnix(const std::string& executable, const std::list& args); - static void runAsyncWindows(const std::string& executable, - const std::list& args); + static int runWindows(const std::string& executable, + const std::list& args, + RunMode runMode); static int runSyncUnix(const std::string& executable, const std::list& args); - static int runSyncWindows(const std::string& executable, - const std::list& args); };