56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
//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
|
|
}
|
|
}
|
|
}
|