How to terminate child process when calling process exits? 
Author Message
 How to terminate child process when calling process exits?

I would like to open a second app programatially using
CreateProcess. Works fine.

But how can I close the child process when my app exits?
TerminateProcess is not recommeneded.

I'd like my child process to behave like Winhelp, which
exits when the app is closed.

Thanks in advance.



Mon, 15 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?
Send it a message asking it (telling it) to shut itself down.


Quote:
> I would like to open a second app programatially using
> CreateProcess. Works fine.

> But how can I close the child process when my app exits?
> TerminateProcess is not recommeneded.

> I'd like my child process to behave like Winhelp, which
> exits when the app is closed.

> Thanks in advance.



Mon, 15 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?
That was just my idea. But I can't figure out how to get
the chid processes mainframe handle from the process handle.

Any idea?

Richard Norman schrieb:

Quote:
> Send it a message asking it (telling it) to shut itself down.



> > I would like to open a second app programatially using
> > CreateProcess. Works fine.

> > But how can I close the child process when my app exits?
> > TerminateProcess is not recommeneded.

> > I'd like my child process to behave like Winhelp, which
> > exits when the app is closed.

> > Thanks in advance.



Mon, 15 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?
Try using FindWindow to get the HWND.


Mon, 15 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?
Uwe Kotyczka schrieb:

Quote:
> I would like to open a second app programatially using
> CreateProcess. Works fine.

> But how can I close the child process when my app exits?
> TerminateProcess is not recommeneded.

> I'd like my child process to behave like Winhelp, which
> exits when the app is closed.

> Thanks in advance.

OK, found it myself:

Terminating a Process
...
If a process is terminated by ExitProcess, the system calls the entry-point function of each
attached DLL with a value indicating that the process is detaching from the DLL. DLLs are not
notified when a process is terminated by TerminateProcess. For more information about DLLs, see
Dynamic-Link Libraries.

Warning  The TerminateProcess function should be used only in extreme circumstances, since it does
not allow threads to clean up or save data and does not notify attached DLLs. If you need to have
one process terminate another process, the following steps provide a better solution:

- Have both processes call theRegisterWindowMessage function to create a private message.
- One process can terminate the other process by broadcasting the private message using
theBroadcastSystemMessage function as follows:
BroadcastSystemMessage(
    BSF_IGNORECURRENTTASK, // do not send message to this process
    BSM_APPLICATIONS,      // broadcast only to applications
    private message,       // message registered in previous step
    wParam,                // message-specific value
    lParam );              // message-specific value
- The process receiving the private message calls ExitProcess to terminate its execution.

Note  When the system is terminating a process, it does not terminate any child processes that the
process has created.



Tue, 16 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?

Quote:

>> I would like to open a second app programatially using
>> CreateProcess. Works fine.

>> But how can I close the child process when my app exits?
>> TerminateProcess is not recommeneded.

This works a treat ...

// Courtesy Microsoft Knowledge Base
// HOWTO: Terminate an Application "Cleanly" in Win32
// Last reviewed: April 2, 1998
// Article ID: Q178893

=========================================================
TerminateApp.h
=========================================================
#define TA_FAILED 0
#define TA_SUCCESS_CLEAN 1
#define TA_SUCCESS_KILL 2
DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout ) ;
=========================================================
TerminateApp.cpp
=========================================================
#include "TerminateApp.h"
#include <vdmdbg.h>
typedef struct
{
  DWORD   dwID ;
  DWORD   dwThread ;

Quote:
} TERMINFO ;

// Declare Callback Enum Functions.
BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) ;
/*----------------------------------------------------------------
DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
Purpose:
  Shut down a 32-Bit Process (or 16-bit process under Windows 95)
Parameters:
  dwPID
     Process ID of the process to shut down.
  dwTimeout
     Wait time in milliseconds before shutting down the process.
Return Value:
  TA_FAILED - If the shutdown failed.
  TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE.
  TA_SUCCESS_KILL - if the process was shut down with
     TerminateProcess().
  NOTE:  See header for these defines.
----------------------------------------------------------------*/
DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
{
  HANDLE   hProc ;
  DWORD   dwRet ;
  // If we can't open the process with PROCESS_TERMINATE rights,
  // then we give up immediately.
  hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, dwPID);
  if(hProc == NULL)
  {
     return TA_FAILED ;
  }
  // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
  // matches your process's.
  EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;
  // Wait on the handle. If it signals, great. If it times out,
  // then you kill it.
  if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
     dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
  else
     dwRet = TA_SUCCESS_CLEAN ;
  CloseHandle(hProc) ;
  return dwRet ;

Quote:
}

BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
{
  DWORD dwID ;
  GetWindowThreadProcessId(hwnd, &dwID) ;
  if(dwID == (DWORD)lParam)
  {
     PostMessage(hwnd, WM_CLOSE, 0, 0) ;
  }
  return TRUE ;

Quote:
}

Pete Story, Kongsberg Defence & Aerospace AS, Simulation & Training
3600 Kongsberg, Norway
Tlf  : (+47) 32 28 69 42



Tue, 16 Jul 2002 03:00:00 GMT  
 How to terminate child process when calling process exits?
If the child has a window with a known class name, you can post a
message to it.  Or you can have the child inherit a file handle from
the parent and you can write to it.  Or you can create an event or
a pipe.  There are a lot of ways to do it.


Quote:
> That was just my idea. But I can't figure out how to get
> the chid processes mainframe handle from the process handle.

> Any idea?

> Richard Norman schrieb:

> > Send it a message asking it (telling it) to shut itself down.



> > > I would like to open a second app programatially using
> > > CreateProcess. Works fine.

> > > But how can I close the child process when my app exits?
> > > TerminateProcess is not recommeneded.

> > > I'd like my child process to behave like Winhelp, which
> > > exits when the app is closed.

> > > Thanks in advance.



Tue, 16 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. terminate or Exit process, need help!

2. Terminate or Exit Process, need help!

3. terminate or exit process, need help!

4. Terminating a child process

5. How to terminate (childof child) process?

6. monitoring the process time of a child process

7. Creating the child process and redirected to parent process

8. Child Process & Parent Process Data :: Win32

9. Catching termination of child process and system() call

10. Catching termination of child process and system() call

11. Process suddenly terminates without notification

12. terminate process

 

 
Powered by phpBB® Forum Software