强杀进程

// ProcessDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
#include <Psapi.h>
#pragma comment(lib,"psapi.lib")


BOOL KillProcess(string strProcessName)
{
	DWORD dwProcessID[256]={0};		//进程ID
	DWORD dwNeed=0;					//当前运行进程的字节
	DWORD dwProcessCount=0;			//当前运行进程的个数
	//枚举所有进程
	EnumProcesses(dwProcessID,sizeof(dwProcessID),&dwNeed);
	dwProcessCount=dwNeed/sizeof(DWORD);
	//打开进程
	HANDLE hProcess=NULL;
	char strPathName[256]={0};		//记录进程路径
	HMODULE hModule=NULL;
	DWORD dwIndex=0;				//记录是否找到进程
	for(DWORD i=0;i<dwProcessCount;++i)
	{
		//打开进程
		 hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_TERMINATE,FALSE,dwProcessID[i]);
		if(hProcess)
		{
			//枚举进程模块
			 EnumProcessModules(hProcess,&hModule,sizeof(HMODULE),&dwNeed);
			 memset(strPathName,0,sizeof(strPathName));
			//获取进程具体路径及名称
			GetProcessImageFileNameA(hProcess,strPathName,sizeof(strPathName));
			cout<<strPathName<<'\t'<<dwProcessID[i]<<endl;
			//关闭进程
			//比较输入路径与搜索路径是否相同
			string strTemp=strPathName;
			if(strTemp.find(strProcessName)!=-1)
			{
				//结束进程
				TerminateProcess(hProcess,0);	
				dwIndex++;
			}
		}
		else
			cout<<"打开进程失败"<<endl;
	}
	if(dwIndex>0)
	{
		CloseHandle(hProcess);
		return TRUE;
	}
	else
		return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
	string strProcessName;
	char ch='a';
	while(ch!=27)
	{
		system("cls");
		cout<<"输入要查杀的进程名:";
		cin>>strProcessName;
		if(KillProcess(strProcessName))
			cout<<endl<<"结束进程成功"<<endl;
		else
			cout<<endl<<"结束进程失败"<<endl;
		
		cout<<endl<<"随意键继续 ESC退出"<<endl;
		ch=getch();
	}
	return 0;
}

  

posted @ 2019-06-02 15:58  gd_沐辰  阅读(351)  评论(0编辑  收藏  举报