C++编写程序使工控机蓝屏

最近想让工控机蓝屏,从网上一顿查找,写了个c++程序,代码不是很长,记录在博客中!原理是将工控机已经开启的所有进程杀死。代码如下:

#include "stdafx.h"
#include   
#include   
#include   
#include   
#include  
using namespace std;  

bool KillProcess(DWORD dwPid)
{
	printf("Kill进程Pid = %d\n", dwPid);
	//关闭进程
	HANDLE killHandle = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION |   // Required by Alpha
		PROCESS_CREATE_THREAD |   // For CreateRemoteThread
		PROCESS_VM_OPERATION |   // For VirtualAllocEx/VirtualFreeEx
		PROCESS_VM_WRITE,             // For WriteProcessMemory);
		FALSE, dwPid);
	if (killHandle == NULL)
		return false;
	TerminateProcess(killHandle, 0);
	return true;
}

bool traverseProcesses(map& _nameID)   
{  
	PROCESSENTRY32 pe32;  
	pe32.dwSize = sizeof(pe32); 

	HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//获取进程快照  
	if(hProcessSnap == INVALID_HANDLE_VALUE) {  
		cout << "CreateToolhelp32Snapshot Error!" << endl;;  
		return false;  
	}  

	BOOL bResult =Process32First(hProcessSnap, &pe32);  

	int num(0);  
	printf("按任意键 + 回车,电脑蓝屏!!!数据无价,请慎重操作!!!");
	getchar();
	while(bResult)   
	{  
		//string name = string(pe32.szExeFile);
		char temp[300];
		WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, -1, temp, sizeof(temp), NULL, NULL);
		string name = string(temp);
		int id = pe32.th32ProcessID;  

		cout << "[" << ++num << "] : " <<"Process Name:" 
			<< name << "  " << "ProcessID:" << id<< endl;  
		KillProcess(id);
		_nameID.insert(pair(name, id)); //字典存储  
		bResult = Process32Next(hProcessSnap,&pe32);  
	}  

	CloseHandle(hProcessSnap);  

	return true;  
}  

int main()  
{  
	map _nameID;  

	if (!traverseProcesses(_nameID)) {  
		cout << "Start Process Error!" << endl;  
	}  
	getchar();
	return 0;  
}

还有一种办法:

打开命令窗口,输入以下命令:

cmd /c for /f %I in ('wmic process get Name')do (wmic process where Name="%I" delete)

你可能感兴趣的:(c及c++)