void FileHandler::Read(std::vector<Process>& processes) { processes.clear(); if (_File) { long file_size = GetFileSize(); if (file_size <= 0) return; char* buf = (char*)malloc(file_size + 1); memset(buf, 0, file_size+1); fread(buf, 1, file_size, _File); const std::string str(buf); free(buf); std::string::size_type prev = 0; std::string::size_type pos = 0; while (true) { pos = str.find_first_of("\r\n", pos); Process process; if (pos == std::string::npos) { std::string exe_file(str.substr(prev)); if (!exe_file.empty()) { process.ExeFile(exe_file); processes.push_back(process); } break; } if (pos > prev) { process.ExeFile(str.substr(prev, pos-prev)); processes.push_back(process); } ++pos; prev = pos; } } }
void FileHandler::Write(const Process& process) { if (_File) { std::string processData(process.ExeFile()); processData.append("\t"); processData.append(process.Filename()); processData.append("\n"); fwrite(processData.c_str(), 1, processData.length(), _File); } }
void ProcessHandler::KillProcess(const Process& process) { std::vector<Process> processes = GetCurrentProcesses(); std::vector<Process>::iterator iter = processes.begin(); for (; iter != processes.end(); ++iter) { if (_stricmp(iter->ExeFile().c_str(), process.ExeFile().c_str()) == 0 || _stricmp(iter->Filename().c_str(), process.Filename().c_str()) == 0) { HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, iter->ProcessID()); if (hProcess) { TerminateProcess(hProcess, 0); } } } }