Files
2025-07-03 08:48:49 +07:00

39 lines
1.1 KiB
C++

#pragma once
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
void killProcessByName(const std::wstring& processName) {
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
std::wcerr << L"CreateToolhelp32Snapshot (of processes) failed" << std::endl;
return;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32)) {
std::wcerr << L"Process32First failed" << std::endl;
CloseHandle(hProcessSnap);
return;
}
do {
if (processName == pe32.szExeFile) {
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pe32.th32ProcessID);
if (hProcess != NULL) {
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
std::wcout << L"Terminated process: " << pe32.szExeFile << std::endl;
}
}
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
}