Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » LocalProcess child process killing in WIndows
LocalProcess child process killing in WIndows [message #44841] Tue, 07 July 2015 10:51 Go to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

Now LocalProcess works very well. However in windows, when a process is killed, child process are not so orphan processes remain working. It happens for example when launching .bat files.

Here it is TerminateChildProcesses(), that could be added into LocalProcess::Kill(), and searches and kills all child processes. As it uses a DWORD instead of a HANDLE, in DoStart() it would be necessary to get the process dwProcessId in addition to the hProcess.

#include <TlHelp32.h>

Vector<DWORD> GetChildProcessList(DWORD processId)
{
	// Gets all processes list
	PROCESSENTRY32 proc;
	Vector<DWORD> child, all, parents;
	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnap == INVALID_HANDLE_VALUE) 
		return child;
	proc.dwSize = sizeof(proc);
	long f = Process32First(hSnap, &proc);
	while (f) {
		all << proc.th32ProcessID;
		parents << proc.th32ParentProcessID;
       	f = Process32Next(hSnap, &proc);
	}
	CloseHandle(hSnap);

	// Gets process child list
	child << processId;
	int init = 0;
	while (true) {
		int count = child.GetCount();
		if (init >= count)
			break;
		for (int cid = init; cid < count; ++cid) {
			for (int i = 0; i < all.GetCount(); ++i) {
				if (parents[i] == child[cid])
					child << all[i];
			}
		}
		init = count;
	}
	child.Remove(0);
	return child;	
}

void TerminateChildProcesses(DWORD dwProcessId, UINT uExitCode) 
{
	Vector<DWORD> child = GetChildProcessList(dwProcessId);
	for (int i = 0; i < child.GetCount(); ++i) {
		HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, child[i]);
		TerminateProcess(hProcess, uExitCode);
	}
}


Best regards
Iñaki

[Updated on: Tue, 07 July 2015 10:52]

Report message to a moderator

Re: LocalProcess child process killing in WIndows [message #44901 is a reply to message #44841] Sun, 19 July 2015 17:55 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Rolling Eyes

Best regards
Iñaki
Re: LocalProcess child process killing in WIndows [message #45247 is a reply to message #44901] Sat, 17 October 2015 14:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Hi,

I have finally got to reviewing this...

I am afraid there is sort of race-condition in the code. Processes can keep running and open more child processes after the snapshot. These would not be terminated....

Mirek
Re: LocalProcess child process killing in WIndows [message #45248 is a reply to message #45247] Sun, 18 October 2015 11:15 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
mirek wrote on Sat, 17 October 2015 14:19
Hi,

I have finally got to reviewing this...

I am afraid there is sort of race-condition in the code. Processes can keep running and open more child processes after the snapshot. These would not be terminated....

Mirek
This is true. However it is better to try to kill them than to kill none Smile.


Best regards
Iñaki
Previous Topic: VectorMap.Get
Next Topic: Print to desired configured printer without dialog
Goto Forum:
  


Current Time: Thu Mar 28 15:32:53 CET 2024

Total time taken to generate the page: 0.01341 seconds