Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 05-30-2006, 04:25 PM   #1 (permalink)
 
Join Date: Oct 2002
Posts: 123
Send a message via ICQ to botserver Send a message via MSN to botserver Send a message via Yahoo to botserver
Default 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
botserver is offline   Reply With Quote
Old 06-03-2006, 08:14 PM   #2 (permalink)
Administrator
 
Zippy's Avatar
 
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,864
Default

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

Zippy is offline   Reply With Quote
Old 06-04-2006, 02:43 AM   #3 (permalink)
 
Join Date: Oct 2002
Posts: 123
Send a message via ICQ to botserver Send a message via MSN to botserver Send a message via Yahoo to botserver
Default

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

http://www.ollydbg.de/
botserver is offline   Reply With Quote
Old 06-04-2006, 06:10 AM   #4 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

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 is offline   Reply With Quote
Old 06-04-2006, 07:41 AM   #5 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

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;
}
noobie is offline   Reply With Quote
Old 06-04-2006, 12:23 PM   #6 (permalink)
 
Join Date: Oct 2002
Posts: 123
Send a message via ICQ to botserver Send a message via MSN to botserver Send a message via Yahoo to botserver
Default

Thanx, i will be try
botserver is offline   Reply With Quote
Old 06-04-2006, 12:46 PM   #7 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

ok, if you have any trouble, let me know.

both work fine for me..

Last edited by noobie; 06-04-2006 at 01:06 PM.
noobie is offline   Reply With Quote
Old 06-04-2006, 03:44 PM   #8 (permalink)
 
Join Date: Oct 2002
Posts: 123
Send a message via ICQ to botserver Send a message via MSN to botserver Send a message via Yahoo to botserver
Default

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.

Last edited by botserver; 06-04-2006 at 04:06 PM.
botserver is offline   Reply With Quote
Old 06-04-2006, 04:43 PM   #9 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

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.
noobie is offline   Reply With Quote
Old 06-04-2006, 11:36 PM   #10 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

Quote:
Originally Posted by botserver
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.
Ravatar is offline   Reply With Quote
Old 07-05-2006, 11:25 AM   #11 (permalink)
 
Join Date: Oct 2002
Posts: 123
Send a message via ICQ to botserver Send a message via MSN to botserver Send a message via Yahoo to botserver
Default

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:
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.
botserver is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5