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
|
#include <Windows.h>
#include <Psapi.h>
#include <iostream>
using std::cout; using std::endl;
#define MAX_PROCS 256
int main()
{
DWORD pProcessIds[MAX_PROCS], nb_Processes, NameSize;
HANDLE hProcess;
char lpBaseName[MAX_PATH], TerminateMe[] = "Notepad.exe";// The process name you want to kill
// Get an array of all of the processes
if (!EnumProcesses(pProcessIds, sizeof(DWORD)*MAX_PROCS, &nb_Processes))
cout << "Enum Processes Failed. Error code: " << GetLastError() << endl;
nb_Processes /= sizeof(DWORD); // Convert from bytes to indexes
for (DWORD i = 0; i < nb_Processes; i++)
{
hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pProcessIds[i]);
if (hProcess != NULL) // If the process couldn't be opened, it's probably a system process and we shouldn't terminate it
{
// This will tell us the name of the current process
NameSize = GetModuleBaseNameA(hProcess, NULL, lpBaseName, MAX_PATH);
// For information only:
lpBaseName[NameSize] = '\0';
cout << "Name: " << lpBaseName << "\tPID:" << pProcessIds[i] << endl;
if (NameSize != 0) // If the name has 0 characters, it's probably not the process your looking for
{
bool match = true;
for (DWORD j = 0; j < NameSize; j++) // Is this name different from the one we're searching for?
if (lpBaseName[i] != TerminateMe[i])
match = false; // Not the droids your looking for
if (match) // If we found it, kill it
{
BOOL nError = TerminateProcess(hProcess, 0);
if (nError)
cout << "Process terminated!" << endl;
else
cout << "Process found but could not be terminated. Error code: " << GetLastError() << endl;
}
}
CloseHandle(hProcess);
}
}
}
|