|
||
|
|
#1 (permalink) |
|
I need kill all child process when i kill the parent process.
Code:
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory ( &si, sizeof( si ) );
si.cb=sizeof ( si );
CreateProcess( "notepad.exe", 0, 0, 0, 0, BELOW_NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi );
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
But if i close my program "ALT + CRTL + DEL Terminate process". Notepad.exe is still runnig. My english sux i known ![]() |
|
|
|
|
|
|
#2 (permalink) |
|
Administrator
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,864
|
I don't think there is any way to do this without the child process killing itself when the other process dies....
__________________
Zippy, Razor Creator and RunUO Core Developer The RunUO Software Team "Intuition, like a flash of lightning, lasts only for a second. It generally comes when one is tormented by a difficult decipherment and when one reviews in his mind the fruitless experiments already tried. Suddenly the light breaks through and one finds after a few minutes what previous days of labor were unable to reveal." ~The Cryptonomicon |
|
|
|
|
|
#3 (permalink) |
|
By example when you open a exe file in Ollydbg for debuging and close ollydbg automatically close the exe file
http://www.ollydbg.de/ |
|
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
in windows, there is no parent-child relationship for processes. however, you can use JobObjects.
try this one: Code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
#include <winnt.h>
int _tmain(int argc, _TCHAR* argv[])
{
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));
si.cb = sizeof(si);
/*
* Create the process suspended.
*/
CreateProcess(NULL, "notepad.exe", 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);
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
Code:
#define _WIN32_WINNT 0x0500 |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
btw, to use the same code with VS 6.0, you need to configure some options:
add VC7 directories for "include files" and make them prior to the others. p.s do not add VC7 directories for libraries. Code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
#include <iostream.h>
#include <winnt.h>
int main( )
{
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( NULL, "notepad.exe", NULL, NULL, FALSE, CREATE_SUSPENDED,NULL, NULL, &si, &pi );
AssignProcessToJobObject(hJob, pi.hProcess);
ResumeThread(pi.hThread);
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
|
|
|
|
|
|
#8 (permalink) |
|
Code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <iostream.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
HANDLE hJob;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { 0 };
hJob = CreateJobObject(NULL, NULL);
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
SetInformationJobObject( hJob, JobObjectExtendedLimitInformation, &jeli, sizeof( jeli ) );
si.cb=sizeof ( si );
CreateProcess( "notepad.exe", 0, 0, 0, 0, CREATE_SUSPENDED, 0, 0, &si, &pi );
AssignProcessToJobObject( hJob, pi.hProcess );
ResumeThread( pi.hThread );
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}
Compile fine but no kill the child process when i close the program. I will try later with VC 6.0 Seem that is a bug in lcc compiler. I reported it in bug forum. Thanx noobie. ![]() Last edited by botserver; 06-04-2006 at 04:06 PM. |
|
|
|
|
|
|
#9 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
I tested it myself with the given compiler, you are right. that doesnt work.
the compiler uses its own libraries and include files, thats the problem. replacing some files might solve the problem or it might even get worse hehe I have tested it with VC7 source files. Some definitions dont exist in old windows.h (VC6), so you need to use it from VC7. Last edited by noobie; 06-04-2006 at 04:49 PM. |
|
|
|
|
|
#10 (permalink) | |
|
Forum Expert
Join Date: Sep 2002
Age: 23
Posts: 1,472
|
Quote:
|
|
|
|
|
|
|
#11 (permalink) | |
|
I did compile in vc6.0 using vc7 include file and work fine.
But JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag is not soported in win2k ![]() From msdn Quote:
![]() Thanx anyway noobie. ![]() |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|