/******************************************************************************* 函数名称 : LookUpProcessAndKill 函数描述 : 遍历进程找到需要杀死的进程 输入参数 : N/A 输出参数 : N/A 返回值 : N/A 作者 : N/A *******************************************************************************/ BOOL LookUpProcessAndKill(const vector<CString> &vecProcessName) { BOOL bRet = FALSE; HANDLE SnapShot, ProcessHandle; SHFILEINFO shSmall; PROCESSENTRY32 ProcessInfo; CString strSearchName; CString strRunProcessName; SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(SnapShot != NULL) { ProcessInfo.dwSize = sizeof(ProcessInfo); // 设置ProcessInfo的大小 BOOL Status = Process32First(SnapShot, &ProcessInfo); //提升升级工具进程权限为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); } while(Status) { // 获取进程文件信息 SHGetFileInfo(ProcessInfo.szExeFile, 0, &shSmall, sizeof(shSmall), SHGFI_ICON|SHGFI_SMALLICON); // 检测进程是否需要关闭 strRunProcessName = ProcessInfo.szExeFile; strRunProcessName.MakeLower(); for (vector<CString>::size_type it = 0; it != vecProcessName.size(); ++ it) { strSearchName = vecProcessName[it]; strSearchName.MakeLower(); if(strSearchName == strRunProcessName) { // 获取进程句柄,强行关闭 ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.th32ProcessID); if (ProcessHandle != NULL) { bRet = TerminateProcess(ProcessHandle, 1); CloseHandle(ProcessHandle); } break; } } // 获取下一个进程的信息 Status = Process32Next(SnapShot, &ProcessInfo); } } return bRet; }
时间: 2024-12-28 10:46:20