#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class MyProcessInfo
{
public:
string PROCESS_NAME;
unsigned int cntThreads;
unsigned int th32ProcessID;
unsigned int th32ParentProcessID;
unsigned int pcPriClassBase;
unsigned int dwPriorityClass;
};
// Forward declarations:
vector<MyProcessInfo> GetProcessList();
vector<MyProcessInfo> GetProcessList()
{
vector<MyProcessInfo> process_list;
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
//printError(TEXT("CreateToolhelp32Snapshot (of processes)"));
return process_list;
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
//printError(TEXT("Process32First")); // show cause of failure
CloseHandle(hProcessSnap); // clean the snapshot object
return process_list;
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
//_tprintf(TEXT("\n\n====================================================="));
//_tprintf(TEXT("\nPROCESS NAME: %s"), pe32.szExeFile);
//_tprintf(TEXT("\n-------------------------------------------------------"));
MyProcessInfo t_process;
wstring w_str = pe32.szExeFile;
string t_str(w_str.begin(), w_str.end());
t_process.PROCESS_NAME = t_str;
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
if (hProcess == NULL)
{
//printError(TEXT("OpenProcess"));
}
else
{
dwPriorityClass = GetPriorityClass(hProcess);
if (!dwPriorityClass)
{
//printError(TEXT("GetPriorityClass"));
}
CloseHandle(hProcess);
}
//_tprintf(TEXT("\n Process ID = 0x%08X"), pe32.th32ProcessID);
//_tprintf(TEXT("\n Thread count = %d"), pe32.cntThreads);
//_tprintf(TEXT("\n Parent process ID = 0x%08X"), pe32.th32ParentProcessID);
//_tprintf(TEXT("\n Priority base = %d"), pe32.pcPriClassBase);
//if (dwPriorityClass)
// _tprintf(TEXT("\n Priority class = %d"), dwPriorityClass);
t_process.th32ProcessID = pe32.th32ProcessID;
t_process.cntThreads = pe32.cntThreads;
t_process.th32ParentProcessID = pe32.th32ParentProcessID;
t_process.pcPriClassBase = pe32.pcPriClassBase;
t_process.dwPriorityClass = dwPriorityClass;
process_list.push_back(t_process);
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return process_list;
}
bool kill_process_by_executable_file_name(string process_name)
{
bool flag = false;
vector<MyProcessInfo> process_list = GetProcessList();
for (int i = 0; i != process_list.size(); i++)
{
if (process_list[i].PROCESS_NAME == process_name)
{
DWORD dwProcessID = process_list[i].th32ProcessID;
HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
::TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
flag = true;
break;
}
}
return flag;
}
int main(int argc, char* argv[])
{
if (argc == 2)
{
string target_process_name = argv[1];
kill_process_by_executable_file_name(target_process_name);
}
else
{
printf("useage:\n> kill_process_by_executable_file_name.exe target.exe\n");
}
return 0;
}