CreateProcess and DETACHED_PROCESS flag
Hi,
I am trying to understand the behaviour of CreateProcess when the
DETACHED_PROCESS flag is enabled.
Here is my sample code:
/***************************************************/
PROCESS_INFORMATION tProcessInformation = {0};
STARTUPINFO tStartupInfo = {0};
int lRetVal;
/* Set startup info */
tStartupInfo.cb = sizeof(STARTUPINFO);
/* Run process */
if((CreateProcess(NULL, "echo OK", NULL, NULL, TRUE,
DETACHED_PROCESS, NULL, NULL, &tStartupInfo, &tProcessInformation)) == 0)
{
printf("Error\n");
}
/* Wait for process to finish */
if(WaitForSingleObject(tProcessInformation.hProcess, INFINITE) != 0)
{
printf("Error\n");
}
/* Get process exit code */
if(GetExitCodeProcess(tProcessInformation.hProcess, &lRetVal) == 0)
{
printf("Error\n");
}
printf("Process exit code is: %ld\n", lRetVal);
/***************************************************/
When run with the DETACHED_PROCESS flag enabled the exit code is 1:
"Process exit code is: 1"
When run with the DETACHED_PROCESS flag disabled the exit code is 0:
"Process exit code is: 0"
Can anybody explain to me what is happening and what does the
DETACHED_PROCESS do exactly? MSDN is not clear enough on this.
Thanks in advance.
Ben