-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdylib_windows.go
109 lines (98 loc) · 2.79 KB
/
dylib_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
99
100
101
102
103
104
105
106
107
108
109
package dylib
import (
"fmt"
"strconv"
"syscall"
)
type LazyDLL struct {
*syscall.LazyDLL
// 加了异常处理的
mySyscall *syscall.LazyProc
}
func NewLazyDLL(name string) *LazyDLL {
l := new(LazyDLL)
l.LazyDLL = syscall.NewLazyDLL(name)
if err := l.Load(); err != nil {
return l
}
// 导入调用的, 实现一个动态调用call的,主要是为了解决异常问题
l.mySyscall = l.LazyDLL.NewProc("MySyscall")
if l.mySyscall.Find() != nil {
l.mySyscall = nil
}
return l
}
// 定义一个相同的
type LazyProc struct {
lzProc *syscall.LazyProc
lzdll *LazyDLL
}
func (d *LazyDLL) NewProc(name string) *LazyProc {
l := new(LazyProc)
l.lzProc = d.LazyDLL.NewProc(name)
l.lzdll = d
return l
}
func (d *LazyDLL) Close() {
if d.Handle() != 0 {
syscall.FreeLibrary(syscall.Handle(d.Handle()))
}
}
func (d *LazyDLL) call(proc *LazyProc, a ...uintptr) (r1, r2 uintptr, lastErr error) {
// 没到找到我封装的那个系统函数,就使用原始的
if d.mySyscall == nil {
return proc.CallOriginal(a...)
}
err := proc.Find()
if err != nil {
fmt.Println("proc \"" + proc.lzProc.Name + "\" not find.")
return 0, 0, syscall.EINVAL
}
addr := proc.Addr()
if addr != 0 {
pLen := uintptr(len(a))
switch pLen {
case 0:
return d.mySyscall.Call(addr, pLen)
case 1:
return d.mySyscall.Call(addr, pLen, a[0])
case 2:
return d.mySyscall.Call(addr, pLen, a[0], a[1])
case 3:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2])
case 4:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3])
case 5:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4])
case 6:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5])
case 7:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6])
case 8:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7])
case 9:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])
case 10:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9])
case 11:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10])
case 12:
return d.mySyscall.Call(addr, pLen, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11])
default:
panic("Call " + proc.lzProc.Name + " with too many arguments " + strconv.Itoa(len(a)) + ".")
}
}
return 0, 0, syscall.EINVAL
}
func (p *LazyProc) Addr() uintptr {
return p.lzProc.Addr()
}
func (p *LazyProc) Find() error {
return p.lzProc.Find()
}
func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
return p.lzdll.call(p, a...)
}
func (p *LazyProc) CallOriginal(a ...uintptr) (r1, r2 uintptr, lastErr error) {
return p.lzProc.Call(a...)
}