forked from gogap/dll_caller
/
dll_caller_windows.go
203 lines (179 loc) · 3.72 KB
/
dll_caller_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
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
package dll_caller
import (
"errors"
"fmt"
"reflect"
"strings"
"syscall"
"unsafe"
)
var (
kernel32, _ = syscall.LoadLibrary("kernel32.dll")
)
type Dll struct {
FileName string
dllHandler syscall.Handle
funcProcs map[string]uintptr
}
type FuncCallResult struct {
Ret1 uintptr
Ret2 uintptr
Errno syscall.Errno
}
func NewDll(fileName string) (dll *Dll, err error) {
newDll := new(Dll)
if newDll.funcProcs == nil {
newDll.funcProcs = make(map[string]uintptr)
}
if err = newDll.LoadLibrary(fileName); err != nil {
return
}
return newDll, nil
}
func (p *Dll) LoadLibrary(fileName string) error {
if handler, e := syscall.LoadLibrary(fileName); e != nil {
return e
} else {
p.dllHandler = handler
}
return nil
}
func (p *Dll) FreeLibrary() error {
if p.IsDllLoaded() {
p.dllHandler = 0
return syscall.FreeLibrary(p.dllHandler)
}
return nil
}
func (p *Dll) IsDllLoaded() bool {
if uintptr(p.dllHandler) == 0 {
return false
}
return true
}
func (p *Dll) InitalFunctions(funcNames ...string) error {
if funcNames == nil {
return nil
}
if !p.IsDllLoaded() {
return errors.New("dll should loaded befor inital functions")
}
if p.funcProcs == nil {
p.funcProcs = make(map[string]uintptr)
}
for _, funcName := range funcNames {
funcName = strings.TrimSpace(funcName)
if funcName == "" {
return errors.New("function name could not be empty")
}
if proc, e := syscall.GetProcAddress(p.dllHandler, funcName); e != nil {
return e
} else {
p.funcProcs[funcName] = proc
}
}
return nil
}
func (p *Dll) Call(funcName string, funcParams ...interface{}) (result FuncCallResult, err error) {
var lenParam uintptr = uintptr(len(funcParams))
if p.funcProcs == nil {
err = errors.New("function address not initaled")
return
}
var funcAddress uintptr
if addr, exist := p.funcProcs[funcName]; !exist {
err = errors.New("function address not exist")
return
} else {
funcAddress = addr
}
var r1, r2 uintptr
var errno syscall.Errno
var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr
for parmIndex, param := range funcParams {
var vPtr uintptr = 0
switch v := param.(type) {
case string:
vPtr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))
case *string:
vPtr = uintptr(unsafe.Pointer(syscall.StringBytePtr(*v)))
case bool:
vPtr = uintptr(unsafe.Pointer(&v))
case int:
vPtr = uintptr(v)
case int8:
vPtr = uintptr(v)
case uint8:
vPtr = uintptr(v)
case *uint8:
vPtr = uintptr(unsafe.Pointer(v))
case int16:
vPtr = uintptr(v)
case uint16:
vPtr = uintptr(v)
case *uint16:
vPtr = uintptr(unsafe.Pointer(v))
case int32:
vPtr = uintptr(v)
case uint32:
vPtr = uintptr(v)
case *uint32:
vPtr = uintptr(unsafe.Pointer(v))
case int64:
vPtr = uintptr(v)
case uint64:
vPtr = uintptr(v)
case *uint64:
vPtr = uintptr(unsafe.Pointer(v))
case float32:
vPtr = uintptr(v)
case float64:
vPtr = uintptr(v)
case []byte:
vPtr = uintptr(unsafe.Pointer(&v[0]))
case uintptr:
ptr, _ := param.(uintptr)
vPtr = ptr
default:
err = fmt.Errorf("unsupport convert type %v to uintptr", reflect.TypeOf(param))
return
}
switch parmIndex + 1 {
case 1:
a1 = vPtr
case 2:
a2 = vPtr
case 3:
a3 = vPtr
case 4:
a4 = vPtr
case 5:
a5 = vPtr
case 6:
a6 = vPtr
case 7:
a7 = vPtr
case 8:
a8 = vPtr
case 9:
a9 = vPtr
case 10:
a10 = vPtr
case 11:
a11 = vPtr
case 12:
a12 = vPtr
case 13:
a13 = vPtr
case 14:
a14 = vPtr
case 15:
a15 = vPtr
}
}
r1, r2, errno = syscall.Syscall15(funcAddress, lenParam, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
result.Ret1 = r1
result.Ret2 = r2
result.Errno = errno
return
}