-
Notifications
You must be signed in to change notification settings - Fork 2
/
hthread.go
126 lines (111 loc) · 3.98 KB
/
hthread.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package wingo
import (
"github.com/rogeecn/wingo/proc"
"syscall"
"unsafe"
"github.com/rogeecn/wingo/co"
"github.com/rogeecn/wingo/errco"
)
// Handle to a thread.
type HTHREAD HANDLE
// ⚠️ You must defer HTHREAD.CloseHandle().
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthread
func GetCurrentThread() HTHREAD {
ret, _, _ := syscall.Syscall(proc.GetCurrentThread.Addr(), 0,
0, 0, 0)
return HTHREAD(ret)
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthreadid
func GetCurrentThreadId() uint32 {
ret, _, _ := syscall.Syscall(proc.GetCurrentThreadId.Addr(), 0,
0, 0, 0)
return uint32(ret)
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
func (hThread HTHREAD) CloseHandle() error {
ret, _, err := syscall.Syscall(proc.CloseHandle.Addr(), 1,
uintptr(hThread), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodethread
func (hThread HTHREAD) GetExitCodeThread() (uint32, error) {
var exitCode uint32
ret, _, err := syscall.Syscall(proc.GetExitCodeThread.Addr(), 2,
uintptr(hThread), uintptr(unsafe.Pointer(&exitCode)), 0)
if ret == 0 {
return 0, errco.ERROR(err)
}
return exitCode, nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocessidofthread
func (hThread HTHREAD) GetProcessIdOfThread() (uint32, error) {
ret, _, err := syscall.Syscall(proc.GetProcessIdOfThread.Addr(), 1,
uintptr(hThread), 0, 0)
if ret == 0 {
return 0, errco.ERROR(err)
}
return uint32(ret), nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadid
func (hThread HTHREAD) GetThreadId() (uint32, error) {
ret, _, err := syscall.Syscall(proc.GetThreadId.Addr(), 1,
uintptr(hThread), 0, 0)
if ret == 0 {
return 0, errco.ERROR(err)
}
return uint32(ret), nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
func (hThread HTHREAD) GetThreadTimes() (
creationTime, exitTime, kernelTime, userTime FILETIME, e error) {
ret, _, err := syscall.Syscall6(proc.GetThreadTimes.Addr(), 5,
uintptr(hThread), uintptr(unsafe.Pointer(&creationTime)),
uintptr(unsafe.Pointer(&exitTime)), uintptr(unsafe.Pointer(&kernelTime)),
uintptr(unsafe.Pointer(&userTime)), 0)
if ret == 0 {
e = errco.ERROR(err)
}
return
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-resumethread
func (hThread HTHREAD) ResumeThread() (uint32, error) {
ret, _, err := syscall.Syscall(proc.ResumeThread.Addr(), 1,
uintptr(hThread), 0, 0)
if int(ret) == -1 {
return 0, errco.ERROR(err)
}
return uint32(ret), nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread
func (hThread HTHREAD) TerminateThread(exitCode uint32) error {
ret, _, err := syscall.Syscall(proc.TerminateThread.Addr(), 2,
uintptr(hThread), uintptr(exitCode), 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-suspendthread
func (hThread HTHREAD) SuspendThread() (uint32, error) {
ret, _, err := syscall.Syscall(proc.SuspendThread.Addr(), 1,
uintptr(hThread), 0, 0)
if int(ret) == -1 {
return 0, errco.ERROR(err)
}
return uint32(ret), nil
}
// Pass -1 for infinite timeout.
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject
func (hThread HTHREAD) WaitForSingleObject(milliseconds uint32) (co.WAIT, error) {
ret, _, err := syscall.Syscall(proc.WaitForSingleObject.Addr(), 2,
uintptr(hThread), uintptr(milliseconds), 0)
if co.WAIT(ret) == co.WAIT_FAILED {
return co.WAIT_FAILED, errco.ERROR(err)
}
return co.WAIT(ret), nil
}