github.com/wencode/hack@v0.2.9/dl/dl_windows.go (about) 1 package dl 2 3 import ( 4 "syscall" 5 ) 6 7 // Implemented in runtime/syscall_windows.goc; we provide jumps to them in our assembly file. 8 func loadlibrary(filename *uint16) (handle uintptr, err syscall.Errno) 9 func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err syscall.Errno) 10 11 func Open(filename string) (lib Lib, err error) { 12 utf16filename, e := syscall.UTF16PtrFromString(filename) 13 if e != nil { 14 err = &DLError{ 15 filename: filename, 16 errstr: "convert to utf16 failed: " + e.Error(), 17 } 18 return 19 } 20 handle, e := loadlibrary(utf16filename) 21 if handle == 0 { 22 err = &DLError{ 23 filename: filename, 24 errstr: "load " + filename + "filed: " + e.Error(), 25 } 26 return 27 } 28 return Lib(handle), nil 29 30 } 31 32 func (lib Lib) Close() { 33 if closeHandle == 0 { 34 return 35 } 36 syscall.Syscall(closeHandle, 1, uintptr(lib), 0, 0) 37 } 38 39 func (lib Lib) Sym(symbol string) uintptr { 40 utf8symbol := uint8ptr(symbol) 41 p, e := getprocaddress(uintptr(lib), utf8symbol) 42 if e != 0 { 43 return 0 44 } 45 return p 46 } 47 48 func uint8ptr(s string) *uint8 { 49 b := make([]byte, len(s)+1) 50 copy(b, s) 51 return &b[0] 52 } 53 54 var ( 55 knlLib Lib 56 closeHandle uintptr 57 ) 58 59 func init() { 60 knlLib, e := Open("kernel32.dll") 61 if e != nil { 62 return 63 } 64 closeHandle = knlLib.Sym("CloseHandle") 65 }