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 previous 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

 
Read Message
Read Message
Read Message
Read Message
Previous Topic: VectorMap.Get
Next Topic: Print to desired configured printer without dialog
Goto Forum:
  


Current Time: Fri Mar 29 06:41:41 CET 2024

Total time taken to generate the page: 0.01122 seconds