Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateProcess: How to allow a child process to kill the parent process

55 views
Skip to first unread message

Jim Brennan

unread,
Jul 8, 2004, 4:34:50 PM7/8/04
to
I have an unusual problem. I need to spawn a child process from a
parent process running as a service, and the child process may or may
not need to stop the service in which the parent process runs. I know
it's an strange thing to want to do, but it's what the design needs to
do. I am attempting to do this with the following code:

DWORD dwExitCode;
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE };
GetStartupInfo( &si );

BOOL bSuccess = CreateProcess( NULL,
(LPTSTR)commandLine.c_str(),
&sa,
NULL,
FALSE,
CREATE_NO_WINDOW | CREATE_DEFAULT_ERROR_MODE |
ABOVE_NORMAL_PRIORITY_CLASS, // creation flags
NULL,
currentDir,
&si,
&pi );

where commandLine and currentDir have been set to the appropriate
values.

I then use the following code:

DWORD dwSuccess = WaitForSingleObject(pi.hProcess, INFINITE);

// Retcode of application
bSuccess = GetExitCodeProcess( pi.hProcess, &dwExitCode );

if ( dwSuccess != WAIT_OBJECT_0 )
{
bSuccess = FALSE;
LPVOID lpMsgBuf;
::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwSuccess,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
msg = "InstallServices::launchApp() - Unknown Error : ";
msg += (LPCTSTR)lpMsgBuf;
theTRACER->log(ExceptionTraces, msg.c_str());

// Free the buffer.
LocalFree( lpMsgBuf );
}

This is required in the case where the child process has not killed
the parent process, so that the parent process knows when the child
process has completed. However, in the case where the child process
does need to kill the parent process, the WaitForSingleObject seems to
be preventing the child from doing so.

Any ideas?

0 new messages