forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/dll_windows.go

720 lines
12 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package windows
import (
"sync"
"sync/atomic"
"syscall"
"unsafe"
)
// We need to use LoadLibrary and GetProcAddress from the Go runtime, because
2024-02-18 10:42:21 +00:00
// the these symbols are loaded by the system linker and are required to
2024-02-18 10:42:21 +00:00
// dynamically load additional symbols. Note that in the Go runtime, these
2024-02-18 10:42:21 +00:00
// return syscall.Handle and syscall.Errno, but these are the same, in fact,
2024-02-18 10:42:21 +00:00
// as windows.Handle and windows.Errno, and we intend to keep these the same.
//go:linkname syscall_loadlibrary syscall.loadlibrary
2024-02-18 10:42:21 +00:00
func syscall_loadlibrary(filename *uint16) (handle Handle, err Errno)
//go:linkname syscall_getprocaddress syscall.getprocaddress
2024-02-18 10:42:21 +00:00
func syscall_getprocaddress(handle Handle, procname *uint8) (proc uintptr, err Errno)
// DLLError describes reasons for DLL load failures.
2024-02-18 10:42:21 +00:00
type DLLError struct {
Err error
2024-02-18 10:42:21 +00:00
ObjName string
Msg string
2024-02-18 10:42:21 +00:00
}
func (e *DLLError) Error() string { return e.Msg }
func (e *DLLError) Unwrap() error { return e.Err }
// A DLL implements access to a single DLL.
2024-02-18 10:42:21 +00:00
type DLL struct {
Name string
2024-02-18 10:42:21 +00:00
Handle Handle
}
// LoadDLL loads DLL file into memory.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Warning: using LoadDLL without an absolute path name is subject to
2024-02-18 10:42:21 +00:00
// DLL preloading attacks. To safely load a system DLL, use LazyDLL
2024-02-18 10:42:21 +00:00
// with System set to true, or use LoadLibraryEx directly.
2024-02-18 10:42:21 +00:00
func LoadDLL(name string) (dll *DLL, err error) {
2024-02-18 10:42:21 +00:00
namep, err := UTF16PtrFromString(name)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h, e := syscall_loadlibrary(namep)
2024-02-18 10:42:21 +00:00
if e != 0 {
2024-02-18 10:42:21 +00:00
return nil, &DLLError{
Err: e,
2024-02-18 10:42:21 +00:00
ObjName: name,
Msg: "Failed to load " + name + ": " + e.Error(),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d := &DLL{
Name: name,
2024-02-18 10:42:21 +00:00
Handle: h,
}
2024-02-18 10:42:21 +00:00
return d, nil
2024-02-18 10:42:21 +00:00
}
// MustLoadDLL is like LoadDLL but panics if load operation failes.
2024-02-18 10:42:21 +00:00
func MustLoadDLL(name string) *DLL {
2024-02-18 10:42:21 +00:00
d, e := LoadDLL(name)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
panic(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return d
2024-02-18 10:42:21 +00:00
}
// FindProc searches DLL d for procedure named name and returns *Proc
2024-02-18 10:42:21 +00:00
// if found. It returns an error if search fails.
2024-02-18 10:42:21 +00:00
func (d *DLL) FindProc(name string) (proc *Proc, err error) {
2024-02-18 10:42:21 +00:00
namep, err := BytePtrFromString(name)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
a, e := syscall_getprocaddress(d.Handle, namep)
2024-02-18 10:42:21 +00:00
if e != 0 {
2024-02-18 10:42:21 +00:00
return nil, &DLLError{
Err: e,
2024-02-18 10:42:21 +00:00
ObjName: name,
Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := &Proc{
Dll: d,
2024-02-18 10:42:21 +00:00
Name: name,
2024-02-18 10:42:21 +00:00
addr: a,
}
2024-02-18 10:42:21 +00:00
return p, nil
2024-02-18 10:42:21 +00:00
}
// MustFindProc is like FindProc but panics if search fails.
2024-02-18 10:42:21 +00:00
func (d *DLL) MustFindProc(name string) *Proc {
2024-02-18 10:42:21 +00:00
p, e := d.FindProc(name)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
panic(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p
2024-02-18 10:42:21 +00:00
}
// FindProcByOrdinal searches DLL d for procedure by ordinal and returns *Proc
2024-02-18 10:42:21 +00:00
// if found. It returns an error if search fails.
2024-02-18 10:42:21 +00:00
func (d *DLL) FindProcByOrdinal(ordinal uintptr) (proc *Proc, err error) {
2024-02-18 10:42:21 +00:00
a, e := GetProcAddressByOrdinal(d.Handle, ordinal)
2024-02-18 10:42:21 +00:00
name := "#" + itoa(int(ordinal))
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, &DLLError{
Err: e,
2024-02-18 10:42:21 +00:00
ObjName: name,
Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := &Proc{
Dll: d,
2024-02-18 10:42:21 +00:00
Name: name,
2024-02-18 10:42:21 +00:00
addr: a,
}
2024-02-18 10:42:21 +00:00
return p, nil
2024-02-18 10:42:21 +00:00
}
// MustFindProcByOrdinal is like FindProcByOrdinal but panics if search fails.
2024-02-18 10:42:21 +00:00
func (d *DLL) MustFindProcByOrdinal(ordinal uintptr) *Proc {
2024-02-18 10:42:21 +00:00
p, e := d.FindProcByOrdinal(ordinal)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
panic(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p
2024-02-18 10:42:21 +00:00
}
// Release unloads DLL d from memory.
2024-02-18 10:42:21 +00:00
func (d *DLL) Release() (err error) {
2024-02-18 10:42:21 +00:00
return FreeLibrary(d.Handle)
2024-02-18 10:42:21 +00:00
}
// A Proc implements access to a procedure inside a DLL.
2024-02-18 10:42:21 +00:00
type Proc struct {
Dll *DLL
2024-02-18 10:42:21 +00:00
Name string
2024-02-18 10:42:21 +00:00
addr uintptr
}
// Addr returns the address of the procedure represented by p.
2024-02-18 10:42:21 +00:00
// The return value can be passed to Syscall to run the procedure.
2024-02-18 10:42:21 +00:00
func (p *Proc) Addr() uintptr {
2024-02-18 10:42:21 +00:00
return p.addr
2024-02-18 10:42:21 +00:00
}
//go:uintptrescapes
// Call executes procedure p with arguments a. It will panic, if more than 15 arguments
2024-02-18 10:42:21 +00:00
// are supplied.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned error is always non-nil, constructed from the result of GetLastError.
2024-02-18 10:42:21 +00:00
// Callers must inspect the primary return value to decide whether an error occurred
2024-02-18 10:42:21 +00:00
// (according to the semantics of the specific function being called) before consulting
2024-02-18 10:42:21 +00:00
// the error. The error will be guaranteed to contain windows.Errno.
2024-02-18 10:42:21 +00:00
func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
2024-02-18 10:42:21 +00:00
switch len(a) {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
return syscall.Syscall(p.Addr(), uintptr(len(a)), 0, 0, 0)
2024-02-18 10:42:21 +00:00
case 1:
2024-02-18 10:42:21 +00:00
return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], 0, 0)
2024-02-18 10:42:21 +00:00
case 2:
2024-02-18 10:42:21 +00:00
return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], 0)
2024-02-18 10:42:21 +00:00
case 3:
2024-02-18 10:42:21 +00:00
return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], a[2])
2024-02-18 10:42:21 +00:00
case 4:
2024-02-18 10:42:21 +00:00
return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0)
2024-02-18 10:42:21 +00:00
case 5:
2024-02-18 10:42:21 +00:00
return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0)
2024-02-18 10:42:21 +00:00
case 6:
2024-02-18 10:42:21 +00:00
return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5])
2024-02-18 10:42:21 +00:00
case 7:
2024-02-18 10:42:21 +00:00
return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], 0, 0)
2024-02-18 10:42:21 +00:00
case 8:
2024-02-18 10:42:21 +00:00
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)
2024-02-18 10:42:21 +00:00
case 9:
2024-02-18 10:42:21 +00:00
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])
2024-02-18 10:42:21 +00:00
case 10:
2024-02-18 10:42:21 +00:00
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)
2024-02-18 10:42:21 +00:00
case 11:
2024-02-18 10:42:21 +00:00
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)
2024-02-18 10:42:21 +00:00
case 12:
2024-02-18 10:42:21 +00:00
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])
2024-02-18 10:42:21 +00:00
case 13:
2024-02-18 10:42:21 +00:00
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)
2024-02-18 10:42:21 +00:00
case 14:
2024-02-18 10:42:21 +00:00
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)
2024-02-18 10:42:21 +00:00
case 15:
2024-02-18 10:42:21 +00:00
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])
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// A LazyDLL implements access to a single DLL.
2024-02-18 10:42:21 +00:00
// It will delay the load of the DLL until the first
2024-02-18 10:42:21 +00:00
// call to its Handle method or to one of its
2024-02-18 10:42:21 +00:00
// LazyProc's Addr method.
2024-02-18 10:42:21 +00:00
type LazyDLL struct {
Name string
// System determines whether the DLL must be loaded from the
2024-02-18 10:42:21 +00:00
// Windows System directory, bypassing the normal DLL search
2024-02-18 10:42:21 +00:00
// path.
2024-02-18 10:42:21 +00:00
System bool
mu sync.Mutex
2024-02-18 10:42:21 +00:00
dll *DLL // non nil once DLL is loaded
2024-02-18 10:42:21 +00:00
}
// Load loads DLL file d.Name into memory. It returns an error if fails.
2024-02-18 10:42:21 +00:00
// Load will not try to load DLL, if it is already loaded into memory.
2024-02-18 10:42:21 +00:00
func (d *LazyDLL) Load() error {
2024-02-18 10:42:21 +00:00
// Non-racy version of:
2024-02-18 10:42:21 +00:00
// if d.dll != nil {
2024-02-18 10:42:21 +00:00
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.mu.Lock()
2024-02-18 10:42:21 +00:00
defer d.mu.Unlock()
2024-02-18 10:42:21 +00:00
if d.dll != nil {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// kernel32.dll is special, since it's where LoadLibraryEx comes from.
2024-02-18 10:42:21 +00:00
// The kernel already special-cases its name, so it's always
2024-02-18 10:42:21 +00:00
// loaded from system32.
2024-02-18 10:42:21 +00:00
var dll *DLL
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if d.Name == "kernel32.dll" {
2024-02-18 10:42:21 +00:00
dll, err = LoadDLL(d.Name)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
dll, err = loadLibraryEx(d.Name, d.System)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Non-racy version of:
2024-02-18 10:42:21 +00:00
// d.dll = dll
2024-02-18 10:42:21 +00:00
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// mustLoad is like Load but panics if search fails.
2024-02-18 10:42:21 +00:00
func (d *LazyDLL) mustLoad() {
2024-02-18 10:42:21 +00:00
e := d.Load()
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
panic(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Handle returns d's module handle.
2024-02-18 10:42:21 +00:00
func (d *LazyDLL) Handle() uintptr {
2024-02-18 10:42:21 +00:00
d.mustLoad()
2024-02-18 10:42:21 +00:00
return uintptr(d.dll.Handle)
2024-02-18 10:42:21 +00:00
}
// NewProc returns a LazyProc for accessing the named procedure in the DLL d.
2024-02-18 10:42:21 +00:00
func (d *LazyDLL) NewProc(name string) *LazyProc {
2024-02-18 10:42:21 +00:00
return &LazyProc{l: d, Name: name}
2024-02-18 10:42:21 +00:00
}
// NewLazyDLL creates new LazyDLL associated with DLL file.
2024-02-18 10:42:21 +00:00
func NewLazyDLL(name string) *LazyDLL {
2024-02-18 10:42:21 +00:00
return &LazyDLL{Name: name}
2024-02-18 10:42:21 +00:00
}
// NewLazySystemDLL is like NewLazyDLL, but will only
2024-02-18 10:42:21 +00:00
// search Windows System directory for the DLL if name is
2024-02-18 10:42:21 +00:00
// a base name (like "advapi32.dll").
2024-02-18 10:42:21 +00:00
func NewLazySystemDLL(name string) *LazyDLL {
2024-02-18 10:42:21 +00:00
return &LazyDLL{Name: name, System: true}
2024-02-18 10:42:21 +00:00
}
// A LazyProc implements access to a procedure inside a LazyDLL.
2024-02-18 10:42:21 +00:00
// It delays the lookup until the Addr method is called.
2024-02-18 10:42:21 +00:00
type LazyProc struct {
Name string
mu sync.Mutex
l *LazyDLL
2024-02-18 10:42:21 +00:00
proc *Proc
}
// Find searches DLL for procedure named p.Name. It returns
2024-02-18 10:42:21 +00:00
// an error if search fails. Find will not search procedure,
2024-02-18 10:42:21 +00:00
// if it is already found and loaded into memory.
2024-02-18 10:42:21 +00:00
func (p *LazyProc) Find() error {
2024-02-18 10:42:21 +00:00
// Non-racy version of:
2024-02-18 10:42:21 +00:00
// if p.proc == nil {
2024-02-18 10:42:21 +00:00
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
2024-02-18 10:42:21 +00:00
p.mu.Lock()
2024-02-18 10:42:21 +00:00
defer p.mu.Unlock()
2024-02-18 10:42:21 +00:00
if p.proc == nil {
2024-02-18 10:42:21 +00:00
e := p.l.Load()
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
proc, e := p.l.dll.FindProc(p.Name)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Non-racy version of:
2024-02-18 10:42:21 +00:00
// p.proc = proc
2024-02-18 10:42:21 +00:00
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// mustFind is like Find but panics if search fails.
2024-02-18 10:42:21 +00:00
func (p *LazyProc) mustFind() {
2024-02-18 10:42:21 +00:00
e := p.Find()
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
panic(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Addr returns the address of the procedure represented by p.
2024-02-18 10:42:21 +00:00
// The return value can be passed to Syscall to run the procedure.
2024-02-18 10:42:21 +00:00
// It will panic if the procedure cannot be found.
2024-02-18 10:42:21 +00:00
func (p *LazyProc) Addr() uintptr {
2024-02-18 10:42:21 +00:00
p.mustFind()
2024-02-18 10:42:21 +00:00
return p.proc.Addr()
2024-02-18 10:42:21 +00:00
}
//go:uintptrescapes
// Call executes procedure p with arguments a. It will panic, if more than 15 arguments
2024-02-18 10:42:21 +00:00
// are supplied. It will also panic if the procedure cannot be found.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned error is always non-nil, constructed from the result of GetLastError.
2024-02-18 10:42:21 +00:00
// Callers must inspect the primary return value to decide whether an error occurred
2024-02-18 10:42:21 +00:00
// (according to the semantics of the specific function being called) before consulting
2024-02-18 10:42:21 +00:00
// the error. The error will be guaranteed to contain windows.Errno.
2024-02-18 10:42:21 +00:00
func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
2024-02-18 10:42:21 +00:00
p.mustFind()
2024-02-18 10:42:21 +00:00
return p.proc.Call(a...)
2024-02-18 10:42:21 +00:00
}
var canDoSearchSystem32Once struct {
sync.Once
2024-02-18 10:42:21 +00:00
v bool
}
func initCanDoSearchSystem32() {
2024-02-18 10:42:21 +00:00
// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
2024-02-18 10:42:21 +00:00
// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
2024-02-18 10:42:21 +00:00
// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
2024-02-18 10:42:21 +00:00
// systems that have KB2533623 installed. To determine whether the
2024-02-18 10:42:21 +00:00
// flags are available, use GetProcAddress to get the address of the
2024-02-18 10:42:21 +00:00
// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
2024-02-18 10:42:21 +00:00
// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
2024-02-18 10:42:21 +00:00
// flags can be used with LoadLibraryEx."
2024-02-18 10:42:21 +00:00
canDoSearchSystem32Once.v = (modkernel32.NewProc("AddDllDirectory").Find() == nil)
2024-02-18 10:42:21 +00:00
}
func canDoSearchSystem32() bool {
2024-02-18 10:42:21 +00:00
canDoSearchSystem32Once.Do(initCanDoSearchSystem32)
2024-02-18 10:42:21 +00:00
return canDoSearchSystem32Once.v
2024-02-18 10:42:21 +00:00
}
func isBaseName(name string) bool {
2024-02-18 10:42:21 +00:00
for _, c := range name {
2024-02-18 10:42:21 +00:00
if c == ':' || c == '/' || c == '\\' {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// loadLibraryEx wraps the Windows LoadLibraryEx function.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If name is not an absolute path, LoadLibraryEx searches for the DLL
2024-02-18 10:42:21 +00:00
// in a variety of automatic locations unless constrained by flags.
2024-02-18 10:42:21 +00:00
// See: https://msdn.microsoft.com/en-us/library/ff919712%28VS.85%29.aspx
2024-02-18 10:42:21 +00:00
func loadLibraryEx(name string, system bool) (*DLL, error) {
2024-02-18 10:42:21 +00:00
loadDLL := name
2024-02-18 10:42:21 +00:00
var flags uintptr
2024-02-18 10:42:21 +00:00
if system {
2024-02-18 10:42:21 +00:00
if canDoSearchSystem32() {
2024-02-18 10:42:21 +00:00
flags = LOAD_LIBRARY_SEARCH_SYSTEM32
2024-02-18 10:42:21 +00:00
} else if isBaseName(name) {
2024-02-18 10:42:21 +00:00
// WindowsXP or unpatched Windows machine
2024-02-18 10:42:21 +00:00
// trying to load "foo.dll" out of the system
2024-02-18 10:42:21 +00:00
// folder, but LoadLibraryEx doesn't support
2024-02-18 10:42:21 +00:00
// that yet on their system, so emulate it.
2024-02-18 10:42:21 +00:00
systemdir, err := GetSystemDirectory()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
loadDLL = systemdir + "\\" + name
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h, err := LoadLibraryEx(loadDLL, 0, flags)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &DLL{Name: name, Handle: h}, nil
2024-02-18 10:42:21 +00:00
}
type errString string
func (s errString) Error() string { return string(s) }