/******************************************************************************* 函數名稱 : 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; }