-
Notifications
You must be signed in to change notification settings - Fork 5
/
windows.go
98 lines (78 loc) · 2.24 KB
/
windows.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// +build windows
package processex
import (
"errors"
"os"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
type winProcesses struct {
processes
}
func newProcessEntry() *windows.ProcessEntry32 {
var entry windows.ProcessEntry32
entry.Size = uint32(unsafe.Sizeof(entry))
return &entry
}
// ------------------------------------------------------------------
func (p *winProcesses) getProcesses() error {
const snapProcess = 0x00000002
handle, err := windows.CreateToolhelp32Snapshot(snapProcess, 0)
if err != nil {
return err
}
defer windows.CloseHandle(handle)
entry := newProcessEntry()
// take first process
err = windows.Process32First(handle, entry)
if err != nil {
return err
}
p.make()
for {
p.processes = append(p.processes, newProcessFromEntry(entry))
if err = windows.Process32Next(handle, entry); err != nil {
// catch syscall.ERROR_NO_MORE_FILES on the end of process list
if err == syscall.ERROR_NO_MORE_FILES {
return nil
}
return err
}
}
}
// ------------------------------------------------------------------
// FindByName - FindByName
func (p *winProcesses) FindByName(name string) ([]*os.Process, []*ProcessEx, error) {
err := p.getProcesses()
if err != nil {
return nil, err
}
return p.find(name, 0)
}
// FindByPID - FindByPID
func (p *winProcesses) FindByPID(pid int) ([]*os.Process, []*ProcessEx, error) {
err := p.getProcesses()
if err != nil {
return nil, err
}
return p.find("", pid)
}
// ------------------------------------------------------------------
type linuxProcesses struct {
processes
}
// ------------------------------------------------------------------
func (p *linuxProcesses) getProcesses() error {
return errors.New("not linux os")
}
// ------------------------------------------------------------------
// FindByName - FindByName
func (p *linuxProcesses) FindByName(name string) ([]*os.Process, []*ProcessEx, error) {
return nil, errors.New("not linux os")
}
// FindByPID - FindByPID
func (p *linuxProcesses) FindByPID(int) ([]*os.Process, []*ProcessEx, error) {
return nil, errors.New("not linux os")
}
// ------------------------------------------------------------------