From 6fab055f435d4a43581ceadde0496249a280c21d Mon Sep 17 00:00:00 2001 From: flysand7 Date: Wed, 10 Jul 2024 04:25:44 +1100 Subject: [PATCH] [os2/process]: Add process list function --- core/os/os2/process.odin | 7 +++++++ core/os/os2/process_windows.odin | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/os/os2/process.odin b/core/os/os2/process.odin index 87199ca7a..1555d555b 100644 --- a/core/os/os2/process.odin +++ b/core/os/os2/process.odin @@ -104,6 +104,13 @@ get_ppid :: proc() -> int { return _get_ppid() } +/* + Obtain ID's of all processes running in the system. +*/ +process_list :: proc(allocator: runtime.Allocator) -> ([]int, Error) { + return _process_list(allocator) +} + Process :: struct { pid: int, diff --git a/core/os/os2/process_windows.odin b/core/os/os2/process_windows.odin index ff99853a3..6e4bd4cac 100644 --- a/core/os/os2/process_windows.odin +++ b/core/os/os2/process_windows.odin @@ -2,6 +2,7 @@ package os2 import "core:sys/windows" +import "base:runtime" _exit :: proc "contextless" (code: int) -> ! { windows.ExitProcess(u32(code)) @@ -44,3 +45,18 @@ _get_ppid :: proc() -> int { } return -1 } + +_process_list :: proc(allocator: runtime.Allocator) -> ([]int, Error) { + pid_list := make([dynamic]int, allocator) + snap := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) + if snap == windows.INVALID_HANDLE_VALUE { + return pid_list[:], _get_platform_error() + } + entry := windows.PROCESSENTRY32W { dwSize = size_of(windows.PROCESSENTRY32W) } + status := windows.Process32FirstW(snap, &entry) + for status { + append(&pid_list, cast(int) entry.th32ProcessID) + status = windows.Process32NextW(snap, &entry) + } + return pid_list[:], nil +}