int ExecuteProcess(string &FullPathToExe, string &Parameters, int SecondsToWait)
{
int iMyCounter = 0, iReturnVal = 0;
DWORD dwExitCode;
if (Parameters.size() != 0 )
{
Parameters.insert(0," ");
}
Parameters = getExeName(FullPathToExe).append(Parameters);
char * pszParam = new char[Parameters.size() + 1];
if (pszParam == 0)
{
return 1;
}
const char* pchrTemp = Parameters.c_str();
strcpy(pszParam, pchrTemp);
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
if (CreateProcess(FullPathToExe.c_str(), pszParam, 0, 0, false,
CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo,
&piProcessInfo) != false)
{
GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
while (dwExitCode == STILL_ACTIVE && SecondsToWait != 0)
{
GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
Sleep(500);
iMyCounter += 500;
if (iMyCounter > (SecondsToWait * 1000))
{
dwExitCode = 0;
}
}
}
else
{
iReturnVal = 2;
}
CloseHandle(piProcessInfo.hProcess);
CloseHandle(piProcessInfo.hThread);
delete[]pszParam;
pszParam = 0;
return iReturnVal;
}
string getExeName(string strFullPathToExe)
{
int Position = strFullPathToExe.find_last_of("\\");
strFullPathToExe = strFullPathToExe.erase(0, Position +1);
return strFullPathToExe;
}