代码如下
#include "stdafx.h"
#include<windows.h>
#include<tlhelp32.h>
#include<string>
#include <iostream>
using namespace std;
int KillProcess(DWORD pid){
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if (hProcess == NULL){
printf("OpenProcess error\n");
}
if (TerminateProcess(hProcess, 0)){
printf("kill process success\n");
return 0;
}
else{
printf("kill process faild\n");
return 0;
}
CloseHandle(hProcess);
}
int GetProcess(){
char buff[1024] = { 0 };
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (hProcessSnap == INVALID_HANDLE_VALUE){
printf("CreateToolhelp32Snapshot error");
return 0;
}
BOOL bProcess = Process32First(hProcessSnap,&pe32);
while (bProcess){
wsprintf(buff, "%s---------%d\r\n", pe32.szExeFile, pe32.th32ProcessID);
printf(buff);
memset(buff, 0x00, 1024);
bProcess = Process32Next(hProcessSnap, &pe32);
}
CloseHandle(hProcessSnap);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc == 2 && strcmp(argv[1], "list") == 0){
GetProcess();
}
if (argc == 3 && strcmp(argv[1], "kill") == 0){
KillProcess(atoi(argv[2]));
}
system("pause");
return 0;
}
|