program termination

This is a discussion on program termination within the Windows Programming forums, part of the Platform Specific Boards category; I made a program (let's say program A) that starts another program (which I did not make, let's say program ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    18

    program termination

    I made a program (let's say program A) that starts another program (which I did not make, let's say program B) using CreateProcess (I already have the process handle).
    What I want to achieve is that when Program A shuts down (no matter if it shut down clean or by an error/whatever) program B shuts down too (kinda like debuggers do).

    thanks in advance,
    -Borre

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    When the program you wrote is ending, you can either send a WM_CLOSE message to the program you want to close down, or you can call TerminateProcess passing the handle of the process you want to end.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    First of all thanks for the reply

    Of course I could use wm_close or terminateprocess, but I need to close down program B no matter how program A was shut down. If a user was to close down program A with the windows process manager, program A would be shut down immediately and it would not have time to close down program B with wm_close or terminateprocess

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Where there isn't any magical way you can control program B from your own program after it's been shut down.

    One last option is to hook the TerminateProcess() API call so that you know for sure when your program is about to be shut down. This would allow you to execute some additional code before your program actually shuts down. Hooking an API call is far from trivial though.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Along with handling WM_CLOSE, you'll want to handle WM_ENDSESSION to catch log-off and system-shutdown events.

    However, when using "End Process" or "End Process Tree" from the "Processes" tab in Windows Task Manager, the process is terminated without notification. This is an issue with any process that calls TerminateProcess on program A as you know.

    Here's an article that discusses: Process Invincibility

    You may also find something like Process Guard usefull as well.

    Or you can try your hand at user-mode API hooking as bithub mentioned.

    gg

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Assuming 2000/XP/2003, there is a nice (and fairly simple) way to achieve this using a job object. No kernel patching, api hooking or even administrator access is required. Error checking has been omitted from the following sample.
    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    
    int main(void)
    {
    	HANDLE                               hJob;
    	JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
    	PROCESS_INFORMATION                  pi   = { 0 };
    	STARTUPINFO                          si   = { 0 };
    
    	/*
    	 * Create a job object.
    	 */
    	hJob = CreateJobObject(NULL, NULL);
    
    	/*
    	 * Causes all processes associated with the job to terminate when the
    	 * last handle to the job is closed.
    	 */
    	jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
    	SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli));
    
    	/*
    	 * Create the process suspended.
    	 */
    	si.cb = sizeof(si);
    	CreateProcess(TEXT("C:\\Windows\\System32\\Notepad.exe"), NULL, NULL, NULL, FALSE, 
    	              CREATE_SUSPENDED, NULL, NULL, &si, &pi);
    
    	/*
    	 * Add the process to our job object.
    	 */
    	AssignProcessToJobObject(hJob, pi.hProcess);
    
    	/*
    	 * Start our suspended process.
    	 */
    	ResumeThread(pi.hThread);
    
    	/*
    	 * At this point, if we are closed, windows will automatically clean up
    	 * by closing any handles we have open. When the handle to the job object
    	 * is closed, any processes belonging to the job will be terminated.
    	 * Note: Grandchild processes automatically become part of the job and
    	 * will also be terminated. This behaviour can be avoided by using the
    	 * JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK limit flag.
    	 */
    
    	/*
    	 * Do what you like here. For demonstration purposes we will just wait
    	 * for the child process to complete. Click our close button to see
    	 * everything in action.
    	 */
    	WaitForSingleObject(pi.hProcess, INFINITE);
    
    	/*
    	 * Cleanup. As mentioned, Windows does this automagically when our process
    	 * exits, but it is good style to do it explicitly. 
    	 */
    	CloseHandle(pi.hThread);
    	CloseHandle(pi.hProcess);
    	CloseHandle(hJob);
    
    	return 0;
    }
    Last edited by anonytmouse; 01-12-2005 at 08:46 PM. Reason: Added information on grandchild processes.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    Thanks for all the help.

    The job thing did the job...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. quick termination of program
    By jobolikescake in forum C Programming
    Replies: 7
    Last Post: 01-20-2002, 09:59 PM
  4. abnormal program termination
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2002, 08:56 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21