36 lines
799 B
Go
36 lines
799 B
Go
//go:build windows
|
|
|
|
package game
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// processes lists the running processes via the Toolhelp32 snapshot API.
|
|
func processes() ([]Process, error) {
|
|
h, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer windows.CloseHandle(h) //nolint:errcheck // best effort
|
|
|
|
var entry windows.ProcessEntry32
|
|
entry.Size = uint32(unsafe.Sizeof(entry))
|
|
if err := windows.Process32First(h, &entry); err != nil {
|
|
return nil, err
|
|
}
|
|
var ps []Process
|
|
for {
|
|
ps = append(ps, Process{
|
|
PID: int(entry.ProcessID),
|
|
Name: windows.UTF16ToString(entry.ExeFile[:]),
|
|
})
|
|
if err := windows.Process32Next(h, &entry); err != nil {
|
|
break // ERROR_NO_MORE_FILES ends the walk
|
|
}
|
|
}
|
|
return ps, nil
|
|
}
|