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.
This commit is contained in:
Robert Knight 2011-08-26 11:56:28 +01:00
parent e5e163e6d5
commit 0134e7d53e
2 changed files with 41 additions and 17 deletions

View File

@ -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<std::string>& args)
{
// TODO - Implement me
return 0;
}
#endif
void ProcessUtils::runAsync(const std::string& executable,
const std::list<std::string>& 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<std::string>& args)
int ProcessUtils::runWindows(const std::string& executable,
const std::list<std::string>& args,
RunMode runMode)
{
std::string commandLine = executable;
for (std::list<std::string>::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

View File

@ -46,6 +46,11 @@ class ProcessUtils
#endif
private:
enum RunMode
{
RunSync,
RunAsync
};
static int runElevatedLinux(const std::string& executable,
const std::list<std::string>& 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<std::string>& args);
static void runAsyncWindows(const std::string& executable,
const std::list<std::string>& args);
static int runWindows(const std::string& executable,
const std::list<std::string>& args,
RunMode runMode);
static int runSyncUnix(const std::string& executable,
const std::list<std::string>& args);
static int runSyncWindows(const std::string& executable,
const std::list<std::string>& args);
};