#include <Tlhelp32.h>
//processName-指定的需要杀死的进程名
BOOL KillProcessByName(CString processName)
{
BOOL bRet = FALSE;
HANDLE SnapShot, ProcessHandle;
SHFILEINFO shSmall;
PROCESSENTRY32 ProcessInfo;
CString strSearchName;
CString strRunProcessName;
//get the process list in the snapshot.
SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (SnapShot != NULL)
{
//提升升级工具进程权限为SE_DEBUG_NAME,否则XP下无法杀掉进程
HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
{
LUID luid;
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
{
TOKEN_PRIVILEGES TokenPrivileges;
TokenPrivileges.PrivilegeCount = 1;
TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
TokenPrivileges.Privileges[0].Luid = luid;
AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, 0, NULL, NULL);
}
CloseHandle(hToken);
}
BOOL Status = Process32First(SnapShot, &ProcessInfo);
while (Status)
{
// 获取进程文件信息
SHGetFileInfo(ProcessInfo.szExeFile, 0, &shSmall, sizeof(shSmall), SHGFI_ICON | SHGFI_SMALLICON);
// 检测进程是否需要关闭
if (processName == ProcessInfo.szExeFile)
{
// 获取进程句柄,强行关闭
ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.th32ProcessID);
if (ProcessHandle != NULL)
{
bRet = TerminateProcess(ProcessHandle, 1);
CloseHandle(ProcessHandle);
}
}
// 获取下一个进程的信息
Status = Process32Next(SnapShot, &ProcessInfo);
}
}
return bRet;
}
int main()
{
KillProcessByName("QQ.exe");
return 0;
}
关闭指定进程
最新推荐文章于 2024-04-02 15:22:12 发布