RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Kill Child Process

botserver

Wanderer
Kill Child Process

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;
}

In this example i run "notepad.exe".
But if i close my program "ALT + CRTL + DEL Terminate process". Notepad.exe is still runnig.

My english sux i known :(
 

Zippy

Razor Creator
I don't think there is any way to do this without the child process killing itself when the other process dies....
 

noobie

Wanderer
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;
}

Add the following to stdafx.h
Code:
#define _WIN32_WINNT 0x0500
 

noobie

Wanderer
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;
}
 

botserver

Wanderer
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;
}
not work. I am using lcc compiler. http://www.cs.virginia.edu/~lcc-win32/
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. :)
 

noobie

Wanderer
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.
 

Ravatar

Knight
botserver said:
By example when you open a exe file in Ollydbg for debuging and close ollydbg automatically close the exe file

http://www.ollydbg.de/

Well, olly does that because it shuts down during a program's debug halt. The process basically suicides when the debugger detaches improperly.
 

botserver

Wanderer
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
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
0x00002000 Causes all processes associated with the job to terminate when the
last handle to the job is closed.
This limit requires use of a JOBOBJECT_EXTENDED_LIMIT_INFORMATION structure.
Its BasicLimitInformation member is a JOBOBJECT_BASIC_LIMIT_INFORMATION
structure.

Windows 2000: This flag is not supported.

I test it in winxp , win98 and win2k and not work in win2k :(

Thanx anyway noobie. :)
 
Top