-
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
-
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.
-
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
-
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.
-
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
-
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;
}
-
Thanks for all the help.
The job thing did the job...