[os2/process]: Add process list function

This commit is contained in:
flysand7
2024-07-10 04:25:44 +11:00
parent 2495f1c39a
commit 6fab055f43
2 changed files with 23 additions and 0 deletions
+7
View File
@@ -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,
+16
View File
@@ -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
}