Browse Source

[os2/process]: Add process list function

flysand7 1 year ago
parent
commit
6fab055f43
2 changed files with 23 additions and 0 deletions
  1. 7 0
      core/os/os2/process.odin
  2. 16 0
      core/os/os2/process_windows.odin

+ 7 - 0
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,

+ 16 - 0
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
+}