Populate cache with all currently running processes
| 118 | |
| 119 | // Populate cache with all currently running processes |
| 120 | BOOL ProcessResolver::PopulateAllProcesses() { |
| 121 | LOG_A(LOG_INFO, "ProcessResolver: Starting to populate cache with all running processes"); |
| 122 | |
| 123 | HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 124 | if (hSnapshot == INVALID_HANDLE_VALUE) { |
| 125 | LOG_A(LOG_ERROR, "ProcessResolver: Failed to create process snapshot: %lu", GetLastError()); |
| 126 | return FALSE; |
| 127 | } |
| 128 | |
| 129 | PROCESSENTRY32 pe32; |
| 130 | pe32.dwSize = sizeof(PROCESSENTRY32); |
| 131 | |
| 132 | // Get the first process |
| 133 | if (!Process32First(hSnapshot, &pe32)) { |
| 134 | LOG_A(LOG_ERROR, "ProcessResolver: Failed to get first process: %lu", GetLastError()); |
| 135 | CloseHandle(hSnapshot); |
| 136 | return FALSE; |
| 137 | } |
| 138 | |
| 139 | int processCount = 0; |
| 140 | int cachedCount = 0; |
| 141 | |
| 142 | try { |
| 143 | do { |
| 144 | processCount++; |
| 145 | DWORD pid = pe32.th32ProcessID; |
| 146 | |
| 147 | // Skip system idle process (PID 0) |
| 148 | if (pid == 0) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | // Check if already in cache |
| 153 | if (containsObject(pid)) { |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | // Create process object and add to cache |
| 158 | Process* process = MakeProcess(pid, targetProcessNames); |
| 159 | if (process != nullptr) { |
| 160 | { |
| 161 | std::lock_guard<std::mutex> lock(cache_mutex); |
| 162 | cache[pid] = *process; |
| 163 | } |
| 164 | |
| 165 | // Clean up the temporary process object |
| 166 | delete process; |
| 167 | cachedCount++; |
| 168 | |
| 169 | // Log progress for large numbers of processes |
| 170 | //if (cachedCount % 50 == 0) { |
| 171 | // LOG_A(LOG_DEBUG, "ProcessResolver: Cached %d processes so far...", cachedCount); |
| 172 | //} |
| 173 | } |
| 174 | else { |
| 175 | // MakeProcess can fail for protected/system processes, this is normal |
| 176 | LOG_A(LOG_DEBUG, "ProcessResolver: Failed to create process object for PID %lu (%ls)", |
| 177 | pid, pe32.szExeFile); |
no test coverage detected