This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
319 lines (266 loc) · 7.34 KB
/
process.go
File metadata and controls
319 lines (266 loc) · 7.34 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//go:build windows
//written by DeepSeek-R1
package main
import (
"log"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
var (
// Kernel32.dll
kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
procCreateFile = kernel32DLL.NewProc("CreateFileW")
procDeviceIoControl = kernel32DLL.NewProc("DeviceIoControl")
procCloseHandle = kernel32DLL.NewProc("CloseHandle")
procCreateThread = kernel32DLL.NewProc("CreateThread")
procTerminateThread = kernel32DLL.NewProc("TerminateThread")
procCreateToolhelp32Snapshot = kernel32DLL.NewProc("CreateToolhelp32Snapshot")
procProcess32First = kernel32DLL.NewProc("Process32FirstW")
procProcess32Next = kernel32DLL.NewProc("Process32NextW")
procThread32First = kernel32DLL.NewProc("Thread32First")
procThread32Next = kernel32DLL.NewProc("Thread32Next")
procOpenThread = kernel32DLL.NewProc("OpenThread")
procOpenProcess = kernel32DLL.NewProc("OpenProcess")
procSuspendThread = kernel32DLL.NewProc("SuspendThread")
procResumeThread = kernel32DLL.NewProc("ResumeThread")
// NTDLL
ntdllDLL = syscall.NewLazyDLL("ntdll.dll")
procNtSuspendProcess = ntdllDLL.NewProc("NtSuspendProcess")
procNtResumeProcess = ntdllDLL.NewProc("NtResumeProcess")
)
const (
TH32CS_SNAPPROCESS = 0x00000002
TH32CS_SNAPTHREAD = 0x00000004
PROCESS_TERMINATE = 0x0001
PROCESS_SUSPEND_RESUME = 0x0800
THREAD_TERMINATE = 0x0001
THREAD_SUSPEND_RESUME = 0x0002
THREAD_QUERY_INFORMATION = 0x0040
MAX_PATH = 260
)
type PROCESSENTRY32 struct {
Size uint32
CntUsage uint32
ProcessID uint32
DefaultHeapID uintptr
ModuleID uint32
CntThreads uint32
ParentProcessID uint32
PriorityClassBase int32
Flags uint32
ExeFile [MAX_PATH]uint16
}
type THREADENTRY32 struct {
Size uint32
CntUsage uint32
ThreadID uint32
OwnerProcessID uint32
BasePriority int32
DeltaPriority int32
Flags uint32
}
// 获取极域进程状态
func GetMythwareProcessState() (uint32, int) {
pid := FindMythwareProcess()
if pid == 0 {
return 0, -1
}
state := GetProcessState(pid)
return pid, state
}
// 查找极域进程
func FindMythwareProcess() uint32 {
snapshot, _, _ := procCreateToolhelp32Snapshot.Call(TH32CS_SNAPPROCESS, 0)
if snapshot == 0 {
return 0
}
defer procCloseHandle.Call(snapshot)
var processEntry PROCESSENTRY32
processEntry.Size = uint32(unsafe.Sizeof(processEntry))
result, _, _ := procProcess32First.Call(snapshot, uintptr(unsafe.Pointer(&processEntry)))
if result == 0 {
return 0
}
for {
processName := syscall.UTF16ToString(processEntry.ExeFile[:])
if processName == "StudentMain.exe" {
return processEntry.ProcessID
}
result, _, _ := procProcess32Next.Call(snapshot, uintptr(unsafe.Pointer(&processEntry)))
if result == 0 {
break
}
}
return 0
}
// 获取进程状态
func GetProcessState(pid uint32) int {
snapshot, _, _ := procCreateToolhelp32Snapshot.Call(TH32CS_SNAPTHREAD, uintptr(pid))
if snapshot == 0 {
return -1
}
defer procCloseHandle.Call(snapshot)
var threadEntry THREADENTRY32
threadEntry.Size = uint32(unsafe.Sizeof(threadEntry))
result, _, _ := procThread32First.Call(snapshot, uintptr(unsafe.Pointer(&threadEntry)))
if result == 0 {
return -1
}
hasRunningThread := false
hasSuspendedThread := false
for {
if threadEntry.OwnerProcessID == pid {
hThread, _, _ := procOpenThread.Call(THREAD_SUSPEND_RESUME|THREAD_QUERY_INFORMATION, 0, uintptr(threadEntry.ThreadID))
if hThread != 0 {
previousSuspendCount, _, _ := procSuspendThread.Call(hThread)
if previousSuspendCount == 0 {
hasRunningThread = true
} else {
hasSuspendedThread = true
}
procResumeThread.Call(hThread)
procCloseHandle.Call(hThread)
}
}
result, _, _ := procThread32Next.Call(snapshot, uintptr(unsafe.Pointer(&threadEntry)))
if result == 0 {
break
}
}
if hasRunningThread {
return 0
} else if hasSuspendedThread {
return 1
}
return -1
}
// 检查极域是否在运行
func IsMythwareRunning() bool {
pid, state := GetMythwareProcessState()
return pid != 0 && state == 0
}
// 检查极域是否暂停
func IsMythwareSuspended() bool {
_, state := GetMythwareProcessState()
return state == 1
}
// 结束极域进程
func KillMythwareProcess() bool {
pid := FindMythwareProcess()
if pid == 0 {
return false
}
return KillProcessByThreads(pid)
}
func KillProcessByThreads(pid uint32) bool {
snapshot, _, _ := procCreateToolhelp32Snapshot.Call(TH32CS_SNAPTHREAD, uintptr(pid))
if snapshot == 0 {
return false
}
defer procCloseHandle.Call(snapshot)
var threadEntry THREADENTRY32
threadEntry.Size = uint32(unsafe.Sizeof(threadEntry))
result, _, _ := procThread32First.Call(snapshot, uintptr(unsafe.Pointer(&threadEntry)))
if result == 0 {
return false
}
success := false
for {
if threadEntry.OwnerProcessID == pid {
hThread, _, _ := procOpenThread.Call(THREAD_TERMINATE, 0, uintptr(threadEntry.ThreadID))
if hThread != 0 {
if ret, _, _ := procTerminateThread.Call(hThread, 0); ret != 0 {
success = true
}
procCloseHandle.Call(hThread)
}
}
result, _, _ := procThread32Next.Call(snapshot, uintptr(unsafe.Pointer(&threadEntry)))
if result == 0 {
break
}
}
return success
}
// 暂停极域进程
func SuspendMythwareProcess() bool {
pid := FindMythwareProcess()
if pid == 0 {
return false
}
hProcess, _, _ := procOpenProcess.Call(PROCESS_SUSPEND_RESUME, 0, uintptr(pid))
if hProcess == 0 {
return false
}
defer procCloseHandle.Call(hProcess)
ret, _, _ := procNtSuspendProcess.Call(hProcess)
return ret == 0
}
// 恢复极域进程
func ResumeMythwareProcess() bool {
pid := FindMythwareProcess()
if pid == 0 {
return false
}
hProcess, _, _ := procOpenProcess.Call(PROCESS_SUSPEND_RESUME, 0, uintptr(pid))
if hProcess == 0 {
return false
}
defer procCloseHandle.Call(hProcess)
ret, _, _ := procNtResumeProcess.Call(hProcess)
return ret == 0
}
// 启动极域进程
func StartMythwareProcess() bool {
path, err := GetMythwareInstallPath()
if err != nil {
log.Printf("获取极域安装路径失败: %v", err)
return false
}
exePath := path + "\\StudentMain.exe"
exePathPtr, _ := syscall.UTF16PtrFromString(exePath)
var startupInfo windows.StartupInfo
var processInfo windows.ProcessInformation
startupInfo.Cb = uint32(unsafe.Sizeof(startupInfo))
err = windows.CreateProcess(
exePathPtr,
nil,
nil,
nil,
false,
0,
nil,
nil,
&startupInfo,
&processInfo,
)
if err != nil {
log.Println(err)
return false
}
windows.CloseHandle(processInfo.Thread)
windows.CloseHandle(processInfo.Process)
return true
}
// 获取极域安装路径
func GetMythwareInstallPath() (string, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE,
`SOFTWARE\WOW6432Node\TopDomain\e-Learning Class Standard\1.00`,
registry.READ)
if err != nil {
key, err = registry.OpenKey(registry.LOCAL_MACHINE,
`SOFTWARE\TopDomain\e-Learning Class Standard\1.00`,
registry.READ)
if err != nil {
return "", err
}
}
defer key.Close()
targetDir, _, err := key.GetStringValue("TargetDirectory")
if err != nil {
return "", err
}
return targetDir, nil
}