//Sample Application that explain process concepts
//Parameters Declaration/Definition
int TimetoWait;
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPTSTR lpszCurrValue,LPTSTR lpszVariable;
TCHAR tchBuf[BUFSIZE];
BOOL fSuccess;
if(argc > 2)
{
printf("InvalidArgument");
ExitProcess(1); //Failure
}
//Get and display an environment variable PATH
lpszCurrValue = ((GetEnvironmentVariable("PATH",tchBuf, BUFSIZE) > 0) ? tchBuf : NULL);
lpszVariable = lpszCurrValue;
//Display the environment variable
while (*lpszVariable)
putchar(*lpszVariable++);
putchar('\n');
//Initialise si and pi
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Create a childProcess
if( !CreateProcess( NULL, // No module name (use command line).
"SomeProcess", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed." );
}
// Wait until child process exits.
if(argc == 2)
{
TIMEOUT = atoi(argv[1]);
ret = WaitForSingleObject( pi.hProcess, TIMEOUT );
if(ret == WAIT_TIMEOUT)
{
TerminateProcess(pi.hProcess);
}
}
else
{
WaitForSingleObject( pi.hProcess, INFINITE );
...
}
ExitProcess(0); //Success
|