-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhhook.go
47 lines (40 loc) · 1.44 KB
/
hhook.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
package wingo
import (
"github.com/rogeecn/wingo/proc"
"syscall"
"github.com/rogeecn/wingo/co"
"github.com/rogeecn/wingo/errco"
)
// A handle to a hook.
//
// Note that this callback is recreated each function call, and the number of
// system callbacks is limited somewhere by the Go runtime.
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#hhook
type HHOOK HANDLE
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexw
func SetWindowsHookEx(idHook co.WH,
userFunc func(code int32, wp WPARAM, lp LPARAM) uintptr,
hMod HINSTANCE, threadId uint32) HHOOK {
ret, _, err := syscall.Syscall6(proc.SetWindowsHookEx.Addr(), 4,
uintptr(idHook), syscall.NewCallback(userFunc),
uintptr(hMod), uintptr(threadId), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return HHOOK(ret)
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-callnexthookex
func (hHook HHOOK) CallNextHookEx(nCode int32, wp WPARAM, lp LPARAM) uintptr {
ret, _, _ := syscall.Syscall6(proc.CallNextHookEx.Addr(), 4,
uintptr(hHook), uintptr(nCode), uintptr(wp), uintptr(lp), 0, 0)
return uintptr(ret)
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-unhookwindowshookex
func (hHook HHOOK) UnhookWindowsHookEx() {
ret, _, err := syscall.Syscall(proc.UnhookWindowsHookEx.Addr(), 1,
uintptr(hHook), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}