--- XYNTService-original.cpp 2008-02-22 15:00:30.000000000 +0000
+++ XYNTService.cpp 2010-03-12 10:32:19.726000000 +0000
-7,6 +7,7 @@
#include <winbase.h>
#include <winsvc.h>
#include <process.h>
+#include <Tlhelp32.h>
const int nBufferSize = 500;
-23,6 +24,7 @@
VOID WINAPI XYNTServiceMain( DWORD dwArgc, LPTSTR *lpszArgv );
VOID WINAPI XYNTServiceHandler( DWORD fdwControl );
+VOID RecursiveEndProcess(HANDLE parentProcess, DWORD parentID);
CRITICAL_SECTION myCS;
-154,6 +156,9 @@
PostThreadMessage(pProcInfo[nIndex].dwThreadId,WM_QUIT,0,0);
// sleep for a while so that the process has a chance to terminate itself
::Sleep(nPauseEnd>0?nPauseEnd:50);
+
+ //kill child processes
+ RecursiveEndProcess(pProcInfo[nIndex].hProcess, pProcInfo[nIndex].dwProcessId);
// terminate the process by force
TerminateProcess(pProcInfo[nIndex].hProcess,0);
try // close handles to avoid ERROR_NO_SYSTEM_RESOURCES
-167,6 +172,42 @@
}
}
+
+void RecursiveEndProcess(HANDLE parentProcess, DWORD parentID)
+{
+ if (parentProcess == NULL)
+ {
+ return;
+ }
+
+ PROCESSENTRY32 pe;
+ memset(&pe, 0, sizeof(PROCESSENTRY32));
+ pe.dwSize = sizeof(PROCESSENTRY32);
+ HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD, parentID);
+
+ if (::Process32First(hSnap, &pe))
+ {
+ BOOL bContinue = TRUE;
+ // kill child processes
+ while (bContinue)
+ {
+ // only kill child processes
+ if (pe.th32ParentProcessID == parentID)
+ {
+ HANDLE hChildProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
+
+ if (hChildProc)
+ {
+ RecursiveEndProcess(hChildProc, pe.th32ProcessID);
+ ::TerminateProcess(hChildProc, 0);
+ ::CloseHandle(hChildProc);
+ }
+ }
+ bContinue = ::Process32Next(hSnap, &pe);
+ }
+ }
+}
+
BOOL BounceProcess(char* pName, int nIndex)
{
// bounce the process with given index