C++杀进程

53 篇文章 0 订阅

源代码:

#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <tlhelp32.h>
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"advapi32.lib")
void EnableDebugPriv() {
	HANDLE hToken;
	TOKEN_PRIVILEGES tkp;
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
	LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
	tkp.PrivilegeCount = 1;
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);
	CloseHandle(hToken);
}
int pskill(int id)   //根据进程ID杀进程
{
	HANDLE hProcess=NULL;
	//打开目标进程
	hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,id);
	if (hProcess==NULL) {
		wprintf(L"\nOpen Process fAiled:%d\n",GetLastError());
		return -1;
	}
	//结束目标进程
	DWORD ret=TerminateProcess(hProcess,0);
	if(ret==0) {
		wprintf(L"%d",GetLastError());
	}
	return -1;
}
int main() {
	//进程列举
	HANDLE hSnApshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnApshot != INVALID_HANDLE_VALUE) {
		PROCESSENTRY32 te = { sizeof(te) };
		BOOL f0k = Process32First(hSnApshot, &te);
		for (; f0k; f0k = Process32Next(hSnApshot, &te)) {
			wprintf(L"Pid: %d %s\n", te.th32ProcessID, te.szExeFile);
		}
	}
	CloseHandle(hSnApshot);
	//杀进程
	wprintf(L"the process's id which you want to kill:");
	int id = 0;
	wscanf(L"%d", &id);
	EnableDebugPriv(); //提升权限
	pskill(id);
	return 0;
}

运行结果:

Pid: 0 [System Process]
Pid: 4 System
Pid: 816 smss.exe
Pid: 872 csrss.exe
Pid: 904 winlogon.exe
Pid: 948 services.exe
Pid: 960 lsass.exe
Pid: 1140 ati2evxx.exe
Pid: 1156 svchost.exe
Pid: 1220 svchost.exe
Pid: 1564 svchost.exe
Pid: 1968 svchost.exe
Pid: 2028 ZhuDongFangYu.exe
Pid: 1384 spoolsv.exe
Pid: 1420 ati2evxx.exe
Pid: 1740 explorer.exe
Pid: 464 SynTPEnh.exe
Pid: 476 360tray.exe
Pid: 596 ctfmon.exe
Pid: 672 360sd.exe
Pid: 428 360rp.exe
Pid: 576 WPService.exe
Pid: 616 FetionPCCS.exe
Pid: 804 LMS.exe
Pid: 1536 svchost.exe
Pid: 2992 wdfmgr.exe
Pid: 3012 UNS.exe
Pid: 3056 vmnat.exe
Pid: 3096 vmnetdhcp.exe
Pid: 3116 vmware-authd.exe
Pid: 3380 FetionPCTray.exe
Pid: 3668 svchost.exe
Pid: 2136 alg.exe
Pid: 2216 QQProtect.exe
Pid: 188 QQ.exe
Pid: 3864 TXPlatform.exe
Pid: 3652 360se.exe
Pid: 236 360se.exe
Pid: 2460 360se.exe
Pid: 2092 Tencentdl.exe
Pid: 2844 360se.exe
Pid: 3904 devenv.exe
Pid: 1520 WPFFontCache_v0400.exe
Pid: 1744 AcroRd32.exe
Pid: 2144 AcroRd32.exe
Pid: 2868 conime.exe
Pid: 2004 vmware.exe
Pid: 5144 vmware-tray.exe
Pid: 4404 eclipse.exe
Pid: 6076 360se.exe
Pid: 5596 MSBuild.exe
Pid: 3700 MSBuild.exe
Pid: 4412 mspdbsrv.exe
Pid: 2712 SogouCloud.exe
Pid: 4148 360se.exe
Pid: 4740 360se.exe
Pid: 5984 vcpkgsrv.exe
Pid: 5380 vcpkgsrv.exe
Pid: 5980 notepad.exe
Pid: 2728 cmd.exe
Pid: 5692 06_3.exe
the process's id which you want to kill:5980
请按任意键继续. . .


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值