C++ 枚举进程信息,并结束指定进程

1.枚举进程,获取信息

DWORD needed;

HANDLE hProcess=NULL;
HMODULE hModule;
wchar_t path[260] = _T("");
HANDLE hToken;


HANDLE  hProcessSnap = NULL;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);


if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
Process32First(hProcessSnap, &pe32);
do
{

hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pe32.th32ProcessID);
if (hProcess)
{
EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
GetModuleFileNameExW(hProcess, hModule, path, sizeof(path));//进程路径

}
pe32.szExeFile//进程名
pe32.th32ProcessID//进程ID

} while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcess);

CloseHandle(hModule);


2.通过进程ID结束进程

HANDLE hProcessHandle;
hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, KillID);

TerminateProcess(hProcessHandle, 4);

C++ 枚举进程信息,并结束指定进程_第1张图片

你可能感兴趣的:(C++,结束进程,枚举进程信息)