To kill a hole tree with ALL!!! childs:
bool __fastcall KillProcessTree(DWORD myprocID, DWORD dwTimeout)
{
bool bRet = true;
HANDLE hWnd;
PROCESSENTRY32 pe;
memset(&pe, 0, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnap = :: CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (::Process32First(hSnap, &pe))
{
BOOL bContinue = TRUE;
// kill child processes
while (bContinue)
{
if (pe.th32ParentProcessID == myprocID)
{
ShowMessage ("Gleich - KILL PID: " + AnsiString(pe.th32ProcessID));
// Rekursion
KillProcessTree(pe.th32ProcessID, dwTimeout);
HANDLE hChildProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
if (hChildProc)
{
GetWindowThreadProcessId(hWnd, &myprocID);
// CLOSE Message s
PostMessage(hWnd, WM_CLOSE, 0, 0) ;
if (WaitForSingleObject(hChildProc, dwTimeout) == WAIT_OBJECT_0)
bRet = true;
else
{
bRet = TerminateProcess(hChildProc, 0);
}
::CloseHandle(hChildProc);
}
}
bContinue = ::Process32Next(hSnap, &pe);
}
// kill the main process
HANDLE hProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, myprocID);
if (hProc)
{
::TerminateProcess(hProc, 1);
::CloseHandle(hProc);
}
}
return bRet;
}