-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib_windows.go
68 lines (62 loc) · 1.51 KB
/
lib_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
//go:build windows && (amd64 || arm64)
package ffi
import (
"fmt"
"syscall"
)
// Load loads a shared library at runtime.
//
// The name can be an absolute path, relative path or just the filename.
// If just the filename is passed, it will use the OS specific search paths.
//
// Example:
//
// var filename string
//
// switch runtime.GOOS {
// case "freebsd", "linux":
// filename = "libraylib.so"
// case "windows":
// filename = "raylib.dll"
// case "darwin":
// filename = "libraylib.dylib"
// }
//
// raylib, err := Load(filename)
func Load(name string) (l Lib, err error) {
handle, err := syscall.LoadLibrary(name)
if err != nil {
err = fmt.Errorf("%s: error loading library: %w", name, err)
}
l.Addr = uintptr(handle)
return
}
// Get retrieves the address of an exported function or variable.
//
// Example:
//
// // C code:
// // int magic_number = 42;
//
// magicNumber, err := lib.Get("magic_number")
// if err != nil {
// panic(err)
// }
//
// // prints 42
// fmt.Println(*(*int32)(unsafe.Pointer(magicNumber)))
//
// // if go vet yells "possible misuse of unsafe.Pointer",
// // you can do the following workaround:
// fmt.Println(**(**int32)(unsafe.Pointer(&magicNumber)))
func (l Lib) Get(name string) (addr uintptr, err error) {
return syscall.GetProcAddress(syscall.Handle(l.Addr), name)
}
// Close deletes a reference to the library. If the reference count is zero,
// the library gets unloaded.
func (l Lib) Close() error {
if l.Addr == 0 {
return nil
}
return syscall.FreeLibrary(syscall.Handle(l.Addr))
}