// ProcessController.cpp: implementation of the CProcessController class.
|
//
|
//////////////////////////////////////////////////////////////////////
|
|
|
//#include "ProcessControl.h"
|
#include "stdafx.h"
|
#include "ProcessController.h"
|
|
#include <tlhelp32.h>
|
#include <stdio.h>
|
|
#ifdef _DEBUG
|
#undef THIS_FILE
|
static char THIS_FILE[]=__FILE__;
|
#define new DEBUG_NEW
|
#endif
|
|
//////////////////////////////////////////////////////////////////////
|
// Construction/Destruction
|
//////////////////////////////////////////////////////////////////////
|
|
CProcessController::CProcessController()
|
{
|
|
}
|
|
DWORD CProcessController::TerminateProcessByName(CString ProcessName)
|
{
|
HANDLE hProcessSnap = NULL;
|
DWORD Return = FALSE;
|
PROCESSENTRY32 pe32 = {0};
|
|
ProcessName.MakeLower();
|
|
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
if (hProcessSnap == INVALID_HANDLE_VALUE)
|
return (DWORD)INVALID_HANDLE_VALUE;
|
|
pe32.dwSize = sizeof(PROCESSENTRY32);
|
|
if (Process32First(hProcessSnap, &pe32))
|
{
|
DWORD Code = 0;
|
DWORD dwPriorityClass;
|
|
do
|
{
|
HANDLE hProcess;
|
|
// Get the actual priority class.
|
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
|
FALSE, pe32.th32ProcessID);
|
dwPriorityClass = GetPriorityClass (hProcess);
|
|
CString Temp = pe32.szExeFile;
|
Temp.MakeLower();
|
|
if (Temp == ProcessName)
|
{
|
if (TerminateProcess(hProcess, 0))
|
{
|
GetExitCodeProcess(hProcess, &Code);
|
}
|
else
|
{
|
return GetLastError();
|
}
|
}
|
|
CloseHandle (hProcess);
|
}
|
while (Process32Next(hProcessSnap, &pe32));
|
Return = TRUE;
|
}
|
else
|
Return = FALSE; // could not walk the list of processes
|
|
// Do not forget to clean up the snapshot object.
|
|
CloseHandle (hProcessSnap);
|
|
return Return;
|
}
|
|
int CProcessController::GetProcessList(PPROCESSLIST pProcessList, int MaxNum)
|
{
|
int Index = 0;
|
HANDLE hProcessSnap = NULL;
|
DWORD Return = FALSE;
|
PROCESSENTRY32 pe32 = {0};
|
|
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
if (hProcessSnap == INVALID_HANDLE_VALUE)
|
return 0;
|
|
pe32.dwSize = sizeof(PROCESSENTRY32);
|
|
if (Process32First(hProcessSnap, &pe32))
|
{
|
DWORD Code = 0;
|
DWORD dwPriorityClass;
|
|
do
|
{
|
HANDLE hProcess;
|
|
// Get the actual priority class.
|
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
|
FALSE, pe32.th32ProcessID);
|
dwPriorityClass = GetPriorityClass (hProcess);
|
|
if (Index < MaxNum)
|
{
|
pProcessList[Index].ProcessName = pe32.szExeFile;
|
Index++;
|
}
|
|
CloseHandle (hProcess);
|
|
}
|
while (Process32Next(hProcessSnap, &pe32));
|
}
|
else
|
return 0;
|
// could not walk the list of processes
|
|
// Do not forget to clean up the snapshot object.
|
|
CloseHandle (hProcessSnap);
|
|
return Index;
|
}
|
|
CProcessController::~CProcessController()
|
{
|
|
}
|