| // Copyright 2026 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "process_tree.h" |
| |
| #ifdef _WIN32 |
| #include <tlhelp32.h> |
| #include <windows.h> |
| #endif |
| |
| #include <stdio.h> |
| |
| #include "util.h" |
| |
| // static |
| std::vector<ProcessTree::ProcessInfo> ProcessTree::GetSystemProcesses() { |
| std::vector<ProcessInfo> processes; |
| #ifdef _WIN32 |
| // Create a snapshot of all processes in the system. |
| HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| if (snapshot == INVALID_HANDLE_VALUE) |
| return processes; |
| |
| PROCESSENTRY32W pe32; |
| pe32.dwSize = sizeof(PROCESSENTRY32W); |
| |
| // Walk the process list using explicit Wide versions to ensure we get |
| // WCHAR strings for szExeFile. |
| if (Process32FirstW(snapshot, &pe32)) { |
| do { |
| processes.push_back({ pe32.th32ProcessID, |
| pe32.th32ParentProcessID, |
| ConvertFromUnicodeString(pe32.szExeFile), |
| {} }); |
| } while (Process32NextW(snapshot, &pe32)); |
| } |
| |
| CloseHandle(snapshot); |
| #endif |
| return processes; |
| } |
| |
| // static |
| void ProcessTree::GetSystemProcessDetails(Pid pid, ProcessDetails* details) { |
| // TODO: Implement command line collection for Windows. |
| // This typically requires OpenProcess + NtQueryInformationProcess |
| // to read the Process Environment Block (PEB). |
| details->collected = true; |
| } |