-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprocess_windows.go
More file actions
51 lines (45 loc) · 1.01 KB
/
Copy pathprocess_windows.go
File metadata and controls
51 lines (45 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//go:build windows
package main
import (
"errors"
"fmt"
"os/exec"
"syscall"
"golang.org/x/sys/windows"
)
const windowsCreateNoWindow = 0x08000000
func hideSubprocessWindow(cmd *exec.Cmd) {
if cmd == nil {
return
}
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.CreationFlags |= windowsCreateNoWindow
}
func processIDRunning(pid int) (bool, error) {
handle, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err != nil {
switch {
case errors.Is(err, windows.ERROR_INVALID_PARAMETER):
return false, nil
case errors.Is(err, windows.ERROR_ACCESS_DENIED):
return true, nil
default:
return false, err
}
}
defer windows.CloseHandle(handle)
state, err := windows.WaitForSingleObject(handle, 0)
if err != nil {
return false, err
}
switch state {
case uint32(windows.WAIT_TIMEOUT):
return true, nil
case windows.WAIT_OBJECT_0:
return false, nil
default:
return false, fmt.Errorf("unexpected process wait state %d", state)
}
}