| 6 |
| 7 | #include <windows.h> |
| 8 | #include <tlhelp32.h> |
| 9 |
| 10 | #include "base/win/scoped_handle.h" |
| 11 | #include "base/win/windows_version.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | ProcessId GetCurrentProcId() { |
| 16 | return ::GetCurrentProcessId(); |
| 17 | } |
| 18 |
| 19 | ProcessHandle GetCurrentProcessHandle() { |
| 20 | return ::GetCurrentProcess(); |
| 21 | } |
| 22 |
| 23 | ProcessId GetProcId(ProcessHandle process) { |
| 24 | if (process == base::kNullProcessHandle) |
| 25 | return 0; |
| 26 | // This returns 0 if we have insufficient rights to query the process handle. |
| 27 | // Invalid handles or non-process handles will cause a hard failure. |
| 28 | ProcessId result = GetProcessId(process); |
| 29 | CHECK(result != 0 || GetLastError() != ERROR_INVALID_HANDLE) |
| 30 | << "process handle = " << process; |
| 31 | return result; |
| 32 | } |
| 33 |
| 34 | ProcessId GetParentProcessId(ProcessHandle process) { |
| 35 | ProcessId child_pid = GetProcId(process); |
| 36 | PROCESSENTRY32 process_entry; |
| 37 | process_entry.dwSize = sizeof(PROCESSENTRY32); |
| 38 |
| 39 | win::ScopedHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
| 40 | if (snapshot.IsValid() && Process32First(snapshot.Get(), &process_entry)) { |
| 41 | do { |
| 42 | if (process_entry.th32ProcessID == child_pid) |
| 43 | return process_entry.th32ParentProcessID; |
| 44 | } while (Process32Next(snapshot.Get(), &process_entry)); |
| 45 | } |
| 46 |
| 47 | // TODO(zijiehe): To match other platforms, -1 (UINT32_MAX) should be returned |
| 48 | // if |child_id| cannot be found in the |snapshot|. |
| 49 | return 0u; |
| 50 | } |
| 51 |
| 52 | } // namespace base |
| 53 |