This commit is contained in:
npc0-hue
2026-08-26 09:56:43 +08:00
parent 2b4974b561
commit 8e02a316fa
93 changed files with 21749 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
//go:build windows
package runtime
import (
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
windowsProcessQueryLimitedInformation = 0x1000
windowsStillActive = 259
)
func processAlivePID(pid int) bool {
if pid <= 0 {
return false
}
handle, err := windows.OpenProcess(windowsProcessQueryLimitedInformation, false, uint32(pid))
if err != nil {
if processSnapshotContainsPID(pid) {
return true
}
return err == syscall.ERROR_ACCESS_DENIED
}
defer windows.CloseHandle(handle)
var exitCode uint32
if err := windows.GetExitCodeProcess(handle, &exitCode); err == nil {
return exitCode == windowsStillActive
}
return processSnapshotContainsPID(pid)
}
func processSnapshotContainsPID(pid int) bool {
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return false
}
defer windows.CloseHandle(snapshot)
var entry windows.ProcessEntry32
entry.Size = uint32(unsafe.Sizeof(entry))
if err := windows.Process32First(snapshot, &entry); err != nil {
return false
}
for {
if entry.ProcessID == uint32(pid) {
return true
}
if err := windows.Process32Next(snapshot, &entry); err != nil {
return false
}
}
}