-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
110 lines (96 loc) · 2.3 KB
/
process.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
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"goinject/w32"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
type WindowsProcess struct {
ProcessID int
ParentProcessID int
Exe string
}
func newWindowsProcess(e *windows.ProcessEntry32) WindowsProcess {
// Find when the string ends for decoding
end := 0
for {
if e.ExeFile[end] == 0 {
break
}
end++
}
return WindowsProcess{
ProcessID: int(e.ProcessID),
ParentProcessID: int(e.ParentProcessID),
Exe: syscall.UTF16ToString(e.ExeFile[:end]),
}
}
func getProcesses() ([]WindowsProcess, error) {
handle, err := windows.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, err
}
defer windows.CloseHandle(handle)
var entry windows.ProcessEntry32
entry.Size = uint32(unsafe.Sizeof(entry))
// get the first process
err = windows.Process32First(handle, &entry)
if err != nil {
return nil, err
}
results := make([]WindowsProcess, 0, 50)
for {
results = append(results, newWindowsProcess(&entry))
err = windows.Process32Next(handle, &entry)
if err != nil {
if err == syscall.ERROR_NO_MORE_FILES {
return results, nil
}
return nil, err
}
}
}
func getProcess(processes []WindowsProcess, name string) *WindowsProcess {
for _, p := range processes {
if strings.ToLower(p.Exe) == strings.ToLower(name) {
return &p
}
}
return nil
}
func getProcessId(processName string) int {
processId := 0
handle := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, 0)
if handle != w32.INVALID_HANDLE {
var processEntry w32.PROCESSENTRY32
processEntry.Size = uint32(unsafe.Sizeof(processEntry))
if w32.Process32First(handle, &processEntry) {
for {
if decodeUtf16ToString(processEntry.ExeFile) == processName {
processId = int(processEntry.ProcessID)
break
}
if !w32.Process32Next(handle, &processEntry) {
break
}
}
}
}
w32.CloseHandle(handle)
return processId
}
func openProcess(processId int) w32.HANDLE {
handle, _ := w32.OpenProcess(w32.PROCESS_ALL_ACCESS, false, uint32(processId))
return handle
}
// returns False if 64-bit process is running on 64-bit OS
func getProcessArch() bool {
handle := windows.CurrentProcess()
var isWow64 bool
err := windows.IsWow64Process(handle, &isWow64)
if err != nil {
panic(err)
}
return isWow64
}