C++关闭桌面,explorer.exe
#include<Windows.h>
#include <TlHelp32.h>
#include"resource.h"
#pragma warning(disable:4996)
void taskkill(const char * name)
{
HANDLE info_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (info_handle == INVALID_HANDLE_VALUE)
{
MessageBox(0, TEXT("获取进程句柄失败!"), TEXT("错误"), 0);
return;
}
PROCESSENTRY32W program_info;
program_info.dwSize = sizeof(PROCESSENTRY32W);
int bResult = Process32FirstW(info_handle, &program_info);
if (!bResult)
{
MessageBox(0, TEXT("获取进程句柄失败!"), TEXT("错误"), 0);
return;
}
char tmp[MAX_PATH];
HANDLE hProcess;
while (bResult)
{
wchar_t *pro_name = program_info.szExeFile;
wcstombs(tmp, pro_name, MAX_PATH);
if (!strcmp(tmp, name))
{
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, program_info.th32ProcessID);
if (hProcess) {
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
break;
}
bResult = Process32Next(info_handle, &program_info);
}
CloseHandle(info_handle);
}
int WINAPI WinMain(HINSTANCE h1, HINSTANCE h2, LPSTR cmd, int show)
{
taskkill("explorer.exe");
}