1
2
3
4
5 package windows
6
7 import (
8 "sync"
9 "sync/atomic"
10 "syscall"
11 "unsafe"
12 )
13
14
15
16
17
18
19
20
21 func syscall_loadlibrary(filename *uint16) (handle Handle, err Errno)
22
23
24 func syscall_getprocaddress(handle Handle, procname *uint8) (proc uintptr, err Errno)
25
26
27 type DLLError struct {
28 Err error
29 ObjName string
30 Msg string
31 }
32
33 func (e *DLLError) Error() string { return e.Msg }
34
35 func (e *DLLError) Unwrap() error { return e.Err }
36
37
38 type DLL struct {
39 Name string
40 Handle Handle
41 }
42
43
44
45
46
47
48 func LoadDLL(name string) (dll *DLL, err error) {
49 namep, err := UTF16PtrFromString(name)
50 if err != nil {
51 return nil, err
52 }
53 h, e := syscall_loadlibrary(namep)
54 if e != 0 {
55 return nil, &DLLError{
56 Err: e,
57 ObjName: name,
58 Msg: "Failed to load " + name + ": " + e.Error(),
59 }
60 }
61 d := &DLL{
62 Name: name,
63 Handle: h,
64 }
65 return d, nil
66 }
67
68
69 func MustLoadDLL(name string) *DLL {
70 d, e := LoadDLL(name)
71 if e != nil {
72 panic(e)
73 }
74 return d
75 }
76
77
78
79 func (d *DLL) FindProc(name string) (proc *Proc, err error) {
80 namep, err := BytePtrFromString(name)
81 if err != nil {
82 return nil, err
83 }
84 a, e := syscall_getprocaddress(d.Handle, namep)
85 if e != 0 {
86 return nil, &DLLError{
87 Err: e,
88 ObjName: name,
89 Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
90 }
91 }
92 p := &Proc{
93 Dll: d,
94 Name: name,
95 addr: a,
96 }
97 return p, nil
98 }
99
100
101 func (d *DLL) MustFindProc(name string) *Proc {
102 p, e := d.FindProc(name)
103 if e != nil {
104 panic(e)
105 }
106 return p
107 }
108
109
110
111 func (d *DLL) FindProcByOrdinal(ordinal uintptr) (proc *Proc, err error) {
112 a, e := GetProcAddressByOrdinal(d.Handle, ordinal)
113 name := "#" + itoa(int(ordinal))
114 if e != nil {
115 return nil, &DLLError{
116 Err: e,
117 ObjName: name,
118 Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
119 }
120 }
121 p := &Proc{
122 Dll: d,
123 Name: name,
124 addr: a,
125 }
126 return p, nil
127 }
128
129
130 func (d *DLL) MustFindProcByOrdinal(ordinal uintptr) *Proc {
131 p, e := d.FindProcByOrdinal(ordinal)
132 if e != nil {
133 panic(e)
134 }
135 return p
136 }
137
138
139 func (d *DLL) Release() (err error) {
140 return FreeLibrary(d.Handle)
141 }
142
143
144 type Proc struct {
145 Dll *DLL
146 Name string
147 addr uintptr
148 }
149
150
151
152 func (p *Proc) Addr() uintptr {
153 return p.addr
154 }
155
156
157
158
159
160
161
162
163
164
165 func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
166 switch len(a) {
167 case 0:
168 return syscall.Syscall(p.Addr(), uintptr(len(a)), 0, 0, 0)
169 case 1:
170 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], 0, 0)
171 case 2:
172 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], 0)
173 case 3:
174 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], a[2])
175 case 4:
176 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0)
177 case 5:
178 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0)
179 case 6:
180 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5])
181 case 7:
182 return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], 0, 0)
183 case 8:
184 return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], 0)
185 case 9:
186 return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])
187 case 10:
188 return syscall.Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], 0, 0)
189 case 11:
190 return syscall.Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], 0)
191 case 12:
192 return syscall.Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11])
193 case 13:
194 return syscall.Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], 0, 0)
195 case 14:
196 return syscall.Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], 0)
197 case 15:
198 return syscall.Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14])
199 default:
200 panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")
201 }
202 }
203
204
205
206
207
208 type LazyDLL struct {
209 Name string
210
211
212
213
214 System bool
215
216 mu sync.Mutex
217 dll *DLL
218 }
219
220
221
222 func (d *LazyDLL) Load() error {
223
224
225 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
226 return nil
227 }
228 d.mu.Lock()
229 defer d.mu.Unlock()
230 if d.dll != nil {
231 return nil
232 }
233
234
235
236
237 var dll *DLL
238 var err error
239 if d.Name == "kernel32.dll" {
240 dll, err = LoadDLL(d.Name)
241 } else {
242 dll, err = loadLibraryEx(d.Name, d.System)
243 }
244 if err != nil {
245 return err
246 }
247
248
249
250 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
251 return nil
252 }
253
254
255 func (d *LazyDLL) mustLoad() {
256 e := d.Load()
257 if e != nil {
258 panic(e)
259 }
260 }
261
262
263 func (d *LazyDLL) Handle() uintptr {
264 d.mustLoad()
265 return uintptr(d.dll.Handle)
266 }
267
268
269 func (d *LazyDLL) NewProc(name string) *LazyProc {
270 return &LazyProc{l: d, Name: name}
271 }
272
273
274 func NewLazyDLL(name string) *LazyDLL {
275 return &LazyDLL{Name: name}
276 }
277
278
279
280
281 func NewLazySystemDLL(name string) *LazyDLL {
282 return &LazyDLL{Name: name, System: true}
283 }
284
285
286
287 type LazyProc struct {
288 Name string
289
290 mu sync.Mutex
291 l *LazyDLL
292 proc *Proc
293 }
294
295
296
297
298 func (p *LazyProc) Find() error {
299
300
301 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
302 p.mu.Lock()
303 defer p.mu.Unlock()
304 if p.proc == nil {
305 e := p.l.Load()
306 if e != nil {
307 return e
308 }
309 proc, e := p.l.dll.FindProc(p.Name)
310 if e != nil {
311 return e
312 }
313
314
315 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
316 }
317 }
318 return nil
319 }
320
321
322 func (p *LazyProc) mustFind() {
323 e := p.Find()
324 if e != nil {
325 panic(e)
326 }
327 }
328
329
330
331
332 func (p *LazyProc) Addr() uintptr {
333 p.mustFind()
334 return p.proc.Addr()
335 }
336
337
338
339
340
341
342
343
344
345
346 func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
347 p.mustFind()
348 return p.proc.Call(a...)
349 }
350
351 var canDoSearchSystem32Once struct {
352 sync.Once
353 v bool
354 }
355
356 func initCanDoSearchSystem32() {
357
358
359
360
361
362
363
364
365 canDoSearchSystem32Once.v = (modkernel32.NewProc("AddDllDirectory").Find() == nil)
366 }
367
368 func canDoSearchSystem32() bool {
369 canDoSearchSystem32Once.Do(initCanDoSearchSystem32)
370 return canDoSearchSystem32Once.v
371 }
372
373 func isBaseName(name string) bool {
374 for _, c := range name {
375 if c == ':' || c == '/' || c == '\\' {
376 return false
377 }
378 }
379 return true
380 }
381
382
383
384
385
386
387
388
389 func loadLibraryEx(name string, system bool) (*DLL, error) {
390 loadDLL := name
391 var flags uintptr
392 if system {
393 if canDoSearchSystem32() {
394 flags = LOAD_LIBRARY_SEARCH_SYSTEM32
395 } else if isBaseName(name) {
396
397
398
399
400 systemdir, err := GetSystemDirectory()
401 if err != nil {
402 return nil, err
403 }
404 loadDLL = systemdir + "\\" + name
405 }
406 }
407 h, err := LoadLibraryEx(loadDLL, 0, flags)
408 if err != nil {
409 return nil, err
410 }
411 return &DLL{Name: name, Handle: h}, nil
412 }
413
414 type errString string
415
416 func (s errString) Error() string { return string(s) }
417
View as plain text