list<DWORD> GLDProcessFunc::getProcessIDList(const list<string> &processNameList) { list<DWORD> processIDList; PROCESSENTRY32 pe = { sizeof(PROCESSENTRY32) }; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { if (Process32First(hSnapshot, &pe)) { while (Process32Next(hSnapshot, &pe)) { string sExeFile = wstringTostring(pe.szExeFile); list<string>::const_iterator iter = find(processNameList.begin(), processNameList.end(), sExeFile); if (iter != processNameList.end()) { processIDList.push_back(pe.th32ProcessID); } Sleep(10); } } CloseHandle(hSnapshot); } return processIDList; }
bool GLDProcessFunc::isProcessRunning(const list<string> &exeNameList) { HANDLE hHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 为当前系统进程建立快照 DWORD dwId = ::GetCurrentProcessId(); // 当前进程的Id if (INVALID_HANDLE_VALUE != hHandle) // 如果快照建立成功 { PROCESSENTRY32 stEntry; stEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hHandle, &stEntry)) //在快照中查找一个进程,stEntry返回进程相关属性和信息 { do { wstring ws(stEntry.szExeFile); string sExeFile = wstringTostring(ws); if (find(exeNameList.begin(), exeNameList.end(), sExeFile) != exeNameList.end()) //if (wcsstr(stEntry.szExeFile, szEXEName)) //比较该进程名称是否与strProcessName相符 { if (dwId != stEntry.th32ProcessID) //如果相等,且该进程的Id与当前进程不相等,则找到 { return true; } } } while(Process32Next(hHandle, &stEntry)); //再快照中查找下一个进程。 } // CloseToolhelp32Snapshot(hHandle); //释放快照句柄。 } return false; }
list<string> GLDProcessFunc::getPathByName(const string &lpProcessName) { list<string> retList; wstring ws = stringTowstring(lpProcessName); HANDLE hHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 为当前系统进程建立快照 DWORD dwId = ::GetCurrentProcessId();// 当前进程的Id if (INVALID_HANDLE_VALUE != hHandle)// 如果快照建立成功 { PROCESSENTRY32 stEntry; stEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hHandle, &stEntry)) //在快照中查找一个进程,stEntry返回进程相关属性和信息 { do{ if (wcsstr(stEntry.szExeFile, ws.c_str())) // 比较该进程名称是否与strProcessName相符 { if (dwId != stEntry.th32ProcessID) // 如果相等,且该进程的Id与当前进程不相等,则找到 { HANDLE h_Process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, stEntry.th32ProcessID); if (h_Process != NULL) { WCHAR name[MAX_PATH + 1] = { 0 }; GetModuleFileNameEx(h_Process, NULL, name, MAX_PATH + 1); retList.push_back(wstringTostring(std::wstring(name))); CloseHandle(h_Process); } } } } while (Process32Next(hHandle, &stEntry)); //再快照中查找下一个进程。 } // CloseToolhelp32Snapshot(hHandle); //释放快照句柄。 } return retList; }
bool GLDProcessFunc::killProcess(const string &lpProcessName) { HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 pe; pe.dwSize = sizeof(PROCESSENTRY32); if (!Process32First(hSnapShot, &pe)) { return FALSE; } bool result = false; string strProcessName = lpProcessName; while (Process32Next(hSnapShot, &pe)) { std::wstring ws(pe.szExeFile); string scTmp = wstringTostring(ws); if (0 == _stricmp(scTmp.c_str(), strProcessName.c_str())) { DWORD dwProcessID = pe.th32ProcessID; if (dwProcessID == ::GetCurrentProcessId()) { continue; } HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID); ::TerminateProcess(hProcess, 0); CloseHandle(hProcess); result = true; } } return result; }
bool GLDProcessFunc::killProcessByAbPath(const string &lpProcessPath) { bool bRet = false; string plPath; std::size_t pathDelim = lpProcessPath.find_last_of("/\\"); if (pathDelim != string::npos) { plPath = lpProcessPath.substr(pathDelim + 1); } HANDLE hHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //为当前系统进程建立快照 DWORD dwId = ::GetCurrentProcessId(); //当前进程的Id if (INVALID_HANDLE_VALUE != hHandle) //如果快照建立成功 { PROCESSENTRY32 stEntry; stEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hHandle, &stEntry)) //在快照中查找一个进程,stEntry返回进程相关属性和信息 { do{ if (wcsstr(stEntry.szExeFile, stringTowstring(plPath).c_str())) // 比较该进程名称是否与strProcessName相符 { if (dwId != stEntry.th32ProcessID) //如果相等,且该进程的Id与当前进程不相等,则找到 { HANDLE h_Process = OpenProcess(PROCESS_ALL_ACCESS, TRUE, stEntry.th32ProcessID); if (h_Process != NULL) { WCHAR name[MAX_PATH + 1] = { 0 }; GetModuleFileNameEx(h_Process, NULL, name, MAX_PATH + 1); wstring ws(name); string path = wstringTostring(ws); if (path.compare(lpProcessPath) == 0) { TerminateProcess(h_Process, 0); CloseHandle(h_Process); bRet = true; } } } } } while (Process32Next(hHandle, &stEntry)); //再快照中查找下一个进程。 } } return bRet; }
string GLDProcessFunc::getNameByID(DWORD processID) { HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); if (hProcess != NULL) { TCHAR* procName = new TCHAR[MAX_PATH]; GetModuleFileNameEx(hProcess, NULL, procName, MAX_PATH); wstring ws(procName); string processName = wstringTostring(ws); std::size_t pathDelim = processName.find_last_of("/\\"); if (pathDelim != string::npos) { return processName.substr(pathDelim + 1); } return ""; } return ""; }
void CStringConverter::wstringTostringstream( wstring const &strSource, stringstream &ss ) { ss.str(wstringTostring(strSource)); }