From 2f765026731d2e78ab0c6307cca981ee67fee93f Mon Sep 17 00:00:00 2001 From: Dave Schuyler Date: Thu, 8 Aug 2002 04:48:23 +0000 Subject: [PATCH] Changed to job objects --- direct/src/directd/directd.cxx | 148 +++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 9 deletions(-) diff --git a/direct/src/directd/directd.cxx b/direct/src/directd/directd.cxx index 5d0ebe601f..d3aa83a158 100644 --- a/direct/src/directd/directd.cxx +++ b/direct/src/directd/directd.cxx @@ -17,6 +17,8 @@ // //////////////////////////////////////////////////////////////////// +#define _WIN32_WINNT 0x0500 + #include "directd.h" /*#include "pandaFramework.h" #include "queuedConnectionManager.h"*/ @@ -36,8 +38,10 @@ #include "pset.h" +//#define old_directd +#undef old_directd - +#ifdef old_directd //[ namespace { // The following is from an MSDN example: @@ -128,7 +132,117 @@ namespace { } } +#else //][ +namespace { + // The following is from an MSDN example: + + #define TA_FAILED 0 + #define TA_SUCCESS_CLEAN 1 + #define TA_SUCCESS_KILL 2 + #define TA_SUCCESS_16 3 + + HANDLE sgJobObject; + BOOL CALLBACK + TerminateAppEnum(HWND hwnd, LPARAM lParam) { + DWORD dwID; + GetWindowThreadProcessId(hwnd, &dwID); + if(dwID == (DWORD)lParam) { + PostMessage(hwnd, WM_CLOSE, 0, 0); + } + return TRUE; + } + + /* + DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout) + + Purpose: + Shut down a 32-Bit Process + + Parameters: + dwPID + Process ID of the process to shut down. + + dwTimeout + Wait time in milliseconds before shutting down the process. + + Return Value: + TA_FAILED - If the shutdown failed. + TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE. + TA_SUCCESS_KILL - if the process was shut down with + TerminateProcess(). + */ + DWORD WINAPI + TerminateApp(DWORD dwPID, DWORD dwTimeout) { + HANDLE hProc; + DWORD dwRet; + + // If we can't open the process with PROCESS_TERMINATE rights, + // then we give up immediately. + hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, dwPID); + if(hProc == NULL) { + return TA_FAILED; + } + + // TerminateAppEnum() posts WM_CLOSE to all windows whose PID + // matches your process's. + EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)dwPID); + + // Wait on the handle. If it signals, great. If it times out, + // then you kill it. + if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0) { + dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED); + } else { + dwRet = TA_SUCCESS_CLEAN; + } + CloseHandle(hProc); + + return dwRet; + } + + /* + Start an application with the command line cmd. + */ + void + StartApp(const string& cmd) { + static inited; + if (!inited) { + inited=1; + sgJobObject=CreateJobObject(0, 0); + if (!sgJobObject) { + nout<<"CreateProcess failed: no sgJobObject"<