首页 > 代码库 > 进程(WINAPI),遍历并查找树状的进程信息,实现控制系统进程

进程(WINAPI),遍历并查找树状的进程信息,实现控制系统进程

#include <TlHelp32.h>
//检索系统全部进程
void showall()
{
	PROCESSENTRY32 pe32 = {0};
	pe32.dwSize = sizeof(pe32);
	//查找进程
	HANDLE hpprocess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//CreateToolhelp32Snapshot创建快照
	BOOL bmore = Process32First(hpprocess,&pe32);//查找
	while (bmore)
	{
		printf("%-25s  %d\n",pe32.szExeFile,pe32.th32ParentProcessID);
		bmore=Process32Next(hpprocess, &pe32);
	}
}
//通过进程名来关闭
void close(char *name)
{
	PROCESSENTRY32 pe32 = { 0 };//进程快照
	pe32.dwSize = sizeof(pe32);//设置机构体大小
	HANDLE hpprocess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	BOOL bmore = Process32First(hpprocess, &pe32);
	while (bmore)
	{
		printf("%s    %d\n", pe32.szExeFile, pe32.th32ParentProcessID);
		if (!strcmp("FsCapture.exe", pe32.szExeFile) )
		{
			puts("\n\nfind,存在该进程!");
			HANDLE hpro = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);//打开进程
			TerminateProcess(hpro, 0);//结束线程    注意此处
		}
		bmore = Process32Next(hpprocess, &pe32);
	}
	printf("\n不存在此进程!\n");
}
//通过进程编号来关闭
void closebyid(unsigned long id)
{
	PROCESSENTRY32 pe32 = { 0 };//进程快照
	pe32.dwSize = sizeof(pe32);//设置机构体大小
	HANDLE hpprocess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	BOOL bmore = Process32First(hpprocess, &pe32);//寻找
	//找全部的进程
	while (bmore)
	{
		if (id==pe32.th32ProcessID)
		{
			puts("find");
			HANDLE hpro = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ParentProcessID);//打开进程
			TerminateProcess(hpro, 0);
		}
		bmore = Process32Next(hpprocess, &pe32);
	}
}
void main()
{
	printf("进程名\t\t\t进程编号\n");
	showall();
	close("FsCapture.exe");//通过进程名来结束进程
	closebyid(4356);//通进程编号来结束进程
	getchar();
}
技术分享

进程(WINAPI),遍历并查找树状的进程信息,实现控制系统进程