#include "stdafx.h" #include <windows.h> #include <TlHelp32.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // 给系统所有进程拍一个快照 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == INVALID_HANDLE_VALUE) { cout<<"获取系统进程快照失败!"<<endl; return -1; } PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)}; BOOL bMore = Process32First(hProcessSnap, &pe32); while (bMore) { _tprintf(_T("%s\n"), pe32.szExeFile); cout<<"进程ID:"<<pe32.th32ProcessID<<endl;
if (_tcscmp(pe32.szExeFile,_T("notepad.exe")) == 0)
{
HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); // 通过进程id获取句柄
TerminateProcess(hHandle,0); // 结束某个进程
CloseHandle(hHandle);
}
bMore = Process32Next(hProcessSnap, &pe32); } CloseHandle(hProcessSnap); return 0; }