1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
#ifndef CKILLPROCESSUNI_CPP
#define CKILLPROCESSUNI_CPP
#include <windows.h>
#include <tlhelp32.h>
//
// Some definitions from NTDDK and other sources
//
typedef LONG NTSTATUS;
typedef LONG KPRIORITY;
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
#define SystemProcessesAndThreadsInformation 5
typedef struct _CLIENT_ID {
DWORD UniqueProcess;
DWORD UniqueThread;
} CLIENT_ID;
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING;
typedef struct _VM_COUNTERS {
SIZE_T PeakVirtualSize;
SIZE_T VirtualSize;
ULONG PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
} VM_COUNTERS;
typedef struct _SYSTEM_THREADS {
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER CreateTime;
ULONG WaitTime;
PVOID StartAddress;
CLIENT_ID ClientId;
KPRIORITY Priority;
KPRIORITY BasePriority;
ULONG ContextSwitchCount;
LONG State;
LONG WaitReason;
} SYSTEM_THREADS, * PSYSTEM_THREADS;
// Note that the size of the SYSTEM_PROCESSES structure is
// different on NT 4 and Win2K, but we don't care about it,
// since we don't access neither IoCounters member nor
//Threads array
typedef struct _SYSTEM_PROCESSES {
ULONG NextEntryDelta;
ULONG ThreadCount;
ULONG Reserved1[6];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ProcessName;
KPRIORITY BasePriority;
ULONG ProcessId;
ULONG InheritedFromProcessId;
ULONG HandleCount;
ULONG Reserved2[2];
VM_COUNTERS VmCounters;
#if _WIN32_WINNT >= 0x500
IO_COUNTERS IoCounters;
#endif
SYSTEM_THREADS Threads[1];
} SYSTEM_PROCESSES, * PSYSTEM_PROCESSES;
class CKillProcess
{
private:
//Functions loaded from Kernel32
typedef HANDLE (WINAPI *PFCreateToolhelp32Snapshot)(
DWORD dwFlags,
DWORD th32ProcessID
);
typedef BOOL (WINAPI *PFProcess32First)(
HANDLE hSnapshot,
LPPROCESSENTRY32 lppe
);
typedef BOOL (WINAPI *PFProcess32Next)(
HANDLE hSnapshot,
LPPROCESSENTRY32 lppe
);
// Native NT API Definitions
typedef NTSTATUS (WINAPI * PFZwQuerySystemInformation)
(UINT, PVOID, ULONG, PULONG);
typedef HANDLE (WINAPI* PFGetProcessHeap)(VOID);
typedef LPVOID (WINAPI* PFHeapAlloc)
(HANDLE,DWORD,SIZE_T);
typedef BOOL (WINAPI* PFHeapFree)(HANDLE,DWORD,LPVOID);
public:
CKillProcess() : FCreateToolhelp32Snapshot(NULL),
FProcess32First(NULL), FProcess32Next(NULL),
m_hKernelLib(NULL),
m_hNTLib(NULL)
{
m_hKernelLib = ::LoadLibraryA("Kernel32");
if (m_hKernelLib)
{
// Find ToolHelp functions
FCreateToolhelp32Snapshot =
(PFCreateToolhelp32Snapshot)
::GetProcAddress(m_hKernelLib,
__TEXT("CreateToolhelp32Snapshot"));
FProcess32First = (PFProcess32First)
::GetProcAddress(m_hKernelLib,
__TEXT("Process32First"));
FProcess32Next = (PFProcess32Next)
::GetProcAddress(m_hKernelLib,
__TEXT("Process32Next"));
}
if(!FCreateToolhelp32Snapshot ||
!FProcess32First || !FProcess32Next)
{ // i.e. we couldn't find the ToolHelp functions,
//so we must be on NT4. Let's load the
// undocumented one instead.
if(!m_hKernelLib)
return; // can't do anything at all without
//the kernel.
m_hNTLib = ::LoadLibraryA("ntdll.dll");
if(m_hNTLib)
{
FQuerySysInfo =
(PFZwQuerySystemInformation)
::GetProcAddress(m_hNTLib,
__TEXT("ZwQuerySystemInformation"));
// load some support funcs from the kernel
FGetProcessHeap = (PFGetProcessHeap)
::GetProcAddress(m_hKernelLib,
__TEXT("GetProcessHeap"));
FHeapAlloc = (PFHeapAlloc)
::GetProcAddress(m_hKernelLib,
__TEXT("HeapAlloc"));
FHeapFree = (PFHeapFree)
::GetProcAddress(m_hKernelLib,
__TEXT("HeapFree"));
}
}
}
~CKillProcess()
{
if(m_hKernelLib)
FreeLibrary(m_hKernelLib);
if(m_hNTLib)
FreeLibrary(m_hNTLib);
}
bool KillProcess(IN const char* pstrProcessName)
{
DWORD dwId;
HANDLE hProcess = FindProcess(pstrProcessName,
dwId);
BOOL bResult;
if(!hProcess)
return false;
// TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
::EnumWindows((WNDENUMPROC)
CKillProcess::TerminateAppEnum,
(LPARAM) dwId);
// Wait on the handle. If it signals, great.
//If it times out, then you kill it.
if(WaitForSingleObject(hProcess, 5000)
!=WAIT_OBJECT_0)
bResult = TerminateProcess(hProcess,0);
else
bResult = TRUE;
CloseHandle(hProcess);
return bResult == TRUE;
}
private:
HANDLE FindProcess(IN const char* pstrProcessName,
OUT DWORD& dwId)
{
if(!m_hKernelLib)
return NULL;
if(FCreateToolhelp32Snapshot && FProcess32First &&
FProcess32Next) // use toolhelpapi
return THFindProcess(pstrProcessName, dwId);
if(FQuerySysInfo && FHeapAlloc &&
FGetProcessHeap && FHeapFree) // use NT api
return NTFindProcess(pstrProcessName, dwId);
// neither one got loaded. Strange.
return NULL;
}
HANDLE THFindProcess(IN const char* pstrProcessName,
OUT DWORD& dwId)
{
HANDLE hSnapShot=NULL;
HANDLE hResult = NULL;
PROCESSENTRY32 processInfo;
char* pstrExeName;
bool bFirst = true;
::ZeroMemory(&processInfo, sizeof(PROCESSENTRY32));
processInfo.dwSize = sizeof(PROCESSENTRY32);
hSnapShot = FCreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, 0);
if(hSnapShot == INVALID_HANDLE_VALUE)
return NULL;
// ok now let's iterate with Process32Next until we
// match up the name of our process
while((bFirst ? FProcess32First(hSnapShot,
&processInfo) : FProcess32Next(hSnapShot,
&processInfo)))
{
bFirst = false;
// we need to check for path... and extract
// just the exe name
pstrExeName = strrchr(processInfo.szExeFile,
'\\');
if(!pstrExeName)
pstrExeName = processInfo.szExeFile;
else
pstrExeName++; // skip the \
// ok now compare against our process name
if(stricmp(pstrExeName, pstrProcessName) == 0)
// wee weee we found it
{
// let's get a HANDLE on it
hResult=OpenProcess(
SYNCHRONIZE|PROCESS_TERMINATE, TRUE,
processInfo.th32ProcessID);
dwId = processInfo.th32ProcessID;
break;
}
} // while(Process32Next(hSnapShot, &processInfo){
if(hSnapShot)
CloseHandle(hSnapShot);
return hResult;
}
HANDLE NTFindProcess(IN const char* pstrProcessName,
OUT DWORD& dwId)
{
HANDLE hHeap = FGetProcessHeap();
NTSTATUS Status;
ULONG cbBuffer = 0x8000;
PVOID pBuffer = NULL;
HANDLE hResult = NULL;
// it is difficult to say a priory which size of
// the buffer will be enough to retrieve all
// information, so we startwith 32K buffer and
// increase its size until we get the
// information successfully
do
{
pBuffer = HeapAlloc(hHeap, 0, cbBuffer);
if (pBuffer == NULL) {
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
Status = FQuerySysInfo(
SystemProcessesAndThreadsInformation,
pBuffer, cbBuffer, NULL);
if (Status == STATUS_INFO_LENGTH_MISMATCH)
{
HeapFree(hHeap, 0, pBuffer);
cbBuffer *= 2;
}
else if (!NT_SUCCESS(Status))
{
HeapFree(hHeap, 0, pBuffer);
SetLastError(Status);
return NULL;
}
}
while (Status == STATUS_INFO_LENGTH_MISMATCH);
PSYSTEM_PROCESSES pProcesses =
(PSYSTEM_PROCESSES)pBuffer;
for (;;)
{
PCWSTR pszProcessName =
pProcesses->ProcessName.Buffer;
if (pszProcessName == NULL)
pszProcessName = L"Idle";
CHAR szProcessName[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, pszProcessName,
-1,szProcessName, MAX_PATH, NULL, NULL);
if(stricmp(szProcessName, pstrProcessName)
== 0) // found it
{
hResult=OpenProcess(
SYNCHRONIZE|PROCESS_TERMINATE, TRUE,
pProcesses->ProcessId);
dwId = pProcesses->ProcessId;
break;
}
if (pProcesses->NextEntryDelta == 0)
break;
// find the address of the next process
// structure
pProcesses = (PSYSTEM_PROCESSES)(
((LPBYTE)pProcesses)
+ pProcesses->NextEntryDelta);
}
HeapFree(hHeap, 0, pBuffer);
return hResult;
}
// callback function for window enumeration
static BOOL CALLBACK TerminateAppEnum( HWND hwnd,
LPARAM lParam )
{
DWORD dwID ;
GetWindowThreadProcessId(hwnd, &dwID) ;
if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
}
return TRUE ;
}
HMODULE m_hNTLib;
HMODULE m_hKernelLib;
// ToolHelp related functions
PFCreateToolhelp32Snapshot FCreateToolhelp32Snapshot;
PFProcess32First FProcess32First;
PFProcess32Next FProcess32Next;
// native NT api functions
PFZwQuerySystemInformation FQuerySysInfo;
PFGetProcessHeap FGetProcessHeap;
PFHeapAlloc FHeapAlloc;
PFHeapFree FHeapFree;
};
#endif |