Can I provide some parameter to CreateProcess...?
Yes, you may pass the desired duration
(N) with the parameter lpCommandLine to the process to be started.
The child process may parse lpCommandLine and set up a timer with the desired
duration
. This timer could for example be a Waitable Timer or simply a thread with
Sleep(duration); // waits the duration
ExitProcess(GetLastError()); // exits the "remote" process
The thread (a thread inside the the child process) terminates the entire child process after duration
. The WaitableTimer idea would need a frequent call to a wait function. See Using Waitable Timer Objects for details.
But: The parent process will remain "in control" all the time. However: You may additionally enter a wait state in the parent process, using a wait function (e.g. WaitForSingleObject, waiting on the child processes handle to actually hibernate the parent process until the child process terminates. On top you may evaluate the return value of the child process by a call to the GetExitCodeProcess function.
The described schemes ensures that the desired duration
is best performed, however, you may also control the duration
from the parent process by means of a named event. See Using Event Objects for details. In such an approach, the parent process may set an event when the duration
"is consumed". The child process waits for that event and terminates when the event was set. This approach may be a little less accurate since the parent doesn't quite know when the childs duration
started.
Example with waitable timer:
Parent process:
...
#define CHILD_DURATION 2000 // 2000 ms
HANDLE hProcess;
char ChildName[MAX_PATH];
char CommandLine[MAX_PATH];
sprintf_s(ChildName,MAX_PATH,"MyChild.exe");
sprintf_s(CommandLine,MAX_PATH,"%d",CHILD_DURATION);
// start the child
hProcess = CreateProcess(ChildProcessName,CommandLine,....
if (0 == hProcess)
{
// error with process creation
printf("CreateProcessfailed (%d)\n", GetLastError());
return GetLastError();
}
// and wait for it to finish
if (WAIT_OBJECT_0 == WaitForSingleObject(hProcess,INFINITE)
{
// child finished
} else
{
// error with wait
printf("WaitForSingleObject failed (%d)\n", GetLastError());
return GetLastError();
}
...
Child process:
int main(int argc, char * argv[])
{
HANDLE hWaitableTimer CreateWaitableTimer(NULL,TRUE,NULL);
if (NULL == hWaitableTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return GetLastError();
}
DWORD dwDuration = atoi(argv[1]);
LARGE_INTEGER liDueTime = -10000 * dwDuration;
// waitable timer due time is in 100 nano second units
// negative values indicate relative time
if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return GetLastError();
}
DWORD dwKeepGoing = TRUE;
// enter the "to do" loop while waiting for the timer...
While (dwKeepGoing)
{
switch (WaitForSingleObject(hTimer,0)
{
case WAIT_OBJECT_0:
// duration over,
// optionally finalize "to do" stuff here
// and end child
dwKeepGoing = FALSE;
case WAIT_TIMEOUT:
// do stuff here
break;
case WAIT_FAILED:
// the wait function has failed
printf("WaitForSingleObject failed (%d)\n", GetLastError());
return GetLastError();
}
}
return 0;
}