1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // DLLError describes reasons for DLL load failures.
15 type DLLError struct {
21 func (e *DLLError) Error() string { return e.Msg }
23 // Implemented in runtime/syscall_windows.goc; we provide jumps to them in our assembly file.
24 func loadlibrary(filename *uint16) (handle uintptr, err syscall.Errno)
25 func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err syscall.Errno)
27 // A DLL implements access to a single DLL.
33 // LoadDLL loads DLL file into memory.
35 // Warning: using LoadDLL without an absolute path name is subject to
36 // DLL preloading attacks. To safely load a system DLL, use LazyDLL
37 // with System set to true, or use LoadLibraryEx directly.
38 func LoadDLL(name string) (dll *DLL, err error) {
39 namep, err := UTF16PtrFromString(name)
43 h, e := loadlibrary(namep)
45 return nil, &DLLError{
48 Msg: "Failed to load " + name + ": " + e.Error(),
58 // MustLoadDLL is like LoadDLL but panics if load operation failes.
59 func MustLoadDLL(name string) *DLL {
67 // FindProc searches DLL d for procedure named name and returns *Proc
68 // if found. It returns an error if search fails.
69 func (d *DLL) FindProc(name string) (proc *Proc, err error) {
70 namep, err := BytePtrFromString(name)
74 a, e := getprocaddress(uintptr(d.Handle), namep)
76 return nil, &DLLError{
79 Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),
90 // MustFindProc is like FindProc but panics if search fails.
91 func (d *DLL) MustFindProc(name string) *Proc {
92 p, e := d.FindProc(name)
99 // Release unloads DLL d from memory.
100 func (d *DLL) Release() (err error) {
101 return FreeLibrary(d.Handle)
104 // A Proc implements access to a procedure inside a DLL.
111 // Addr returns the address of the procedure represented by p.
112 // The return value can be passed to Syscall to run the procedure.
113 func (p *Proc) Addr() uintptr {
119 // Call executes procedure p with arguments a. It will panic, if more then 15 arguments
122 // The returned error is always non-nil, constructed from the result of GetLastError.
123 // Callers must inspect the primary return value to decide whether an error occurred
124 // (according to the semantics of the specific function being called) before consulting
125 // the error. The error will be guaranteed to contain windows.Errno.
126 func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
129 return syscall.Syscall(p.Addr(), uintptr(len(a)), 0, 0, 0)
131 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], 0, 0)
133 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], 0)
135 return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], a[2])
137 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0)
139 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0)
141 return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5])
143 return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], 0, 0)
145 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)
147 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])
149 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)
151 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)
153 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])
155 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)
157 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)
159 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])
161 panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")
166 // A LazyDLL implements access to a single DLL.
167 // It will delay the load of the DLL until the first
168 // call to its Handle method or to one of its
169 // LazyProc's Addr method.
170 type LazyDLL struct {
173 // System determines whether the DLL must be loaded from the
174 // Windows System directory, bypassing the normal DLL search
179 dll *DLL // non nil once DLL is loaded
182 // Load loads DLL file d.Name into memory. It returns an error if fails.
183 // Load will not try to load DLL, if it is already loaded into memory.
184 func (d *LazyDLL) Load() error {
185 // Non-racy version of:
187 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
196 // kernel32.dll is special, since it's where LoadLibraryEx comes from.
197 // The kernel already special-cases its name, so it's always
198 // loaded from system32.
201 if d.Name == "kernel32.dll" {
202 dll, err = LoadDLL(d.Name)
204 dll, err = loadLibraryEx(d.Name, d.System)
210 // Non-racy version of:
212 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
216 // mustLoad is like Load but panics if search fails.
217 func (d *LazyDLL) mustLoad() {
224 // Handle returns d's module handle.
225 func (d *LazyDLL) Handle() uintptr {
227 return uintptr(d.dll.Handle)
230 // NewProc returns a LazyProc for accessing the named procedure in the DLL d.
231 func (d *LazyDLL) NewProc(name string) *LazyProc {
232 return &LazyProc{l: d, Name: name}
235 // NewLazyDLL creates new LazyDLL associated with DLL file.
236 func NewLazyDLL(name string) *LazyDLL {
237 return &LazyDLL{Name: name}
240 // NewLazySystemDLL is like NewLazyDLL, but will only
241 // search Windows System directory for the DLL if name is
242 // a base name (like "advapi32.dll").
243 func NewLazySystemDLL(name string) *LazyDLL {
244 return &LazyDLL{Name: name, System: true}
247 // A LazyProc implements access to a procedure inside a LazyDLL.
248 // It delays the lookup until the Addr method is called.
249 type LazyProc struct {
257 // Find searches DLL for procedure named p.Name. It returns
258 // an error if search fails. Find will not search procedure,
259 // if it is already found and loaded into memory.
260 func (p *LazyProc) Find() error {
261 // Non-racy version of:
262 // if p.proc == nil {
263 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
271 proc, e := p.l.dll.FindProc(p.Name)
275 // Non-racy version of:
277 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
283 // mustFind is like Find but panics if search fails.
284 func (p *LazyProc) mustFind() {
291 // Addr returns the address of the procedure represented by p.
292 // The return value can be passed to Syscall to run the procedure.
293 func (p *LazyProc) Addr() uintptr {
300 // Call executes procedure p with arguments a. It will panic, if more then 15 arguments
303 // The returned error is always non-nil, constructed from the result of GetLastError.
304 // Callers must inspect the primary return value to decide whether an error occurred
305 // (according to the semantics of the specific function being called) before consulting
306 // the error. The error will be guaranteed to contain windows.Errno.
307 func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
309 return p.proc.Call(a...)
312 var canDoSearchSystem32Once struct {
317 func initCanDoSearchSystem32() {
318 // https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
319 // "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
320 // Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
321 // systems that have KB2533623 installed. To determine whether the
322 // flags are available, use GetProcAddress to get the address of the
323 // AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
324 // function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
325 // flags can be used with LoadLibraryEx."
326 canDoSearchSystem32Once.v = (modkernel32.NewProc("AddDllDirectory").Find() == nil)
329 func canDoSearchSystem32() bool {
330 canDoSearchSystem32Once.Do(initCanDoSearchSystem32)
331 return canDoSearchSystem32Once.v
334 func isBaseName(name string) bool {
335 for _, c := range name {
336 if c == ':' || c == '/' || c == '\\' {
343 // loadLibraryEx wraps the Windows LoadLibraryEx function.
345 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
347 // If name is not an absolute path, LoadLibraryEx searches for the DLL
348 // in a variety of automatic locations unless constrained by flags.
349 // See: https://msdn.microsoft.com/en-us/library/ff919712%28VS.85%29.aspx
350 func loadLibraryEx(name string, system bool) (*DLL, error) {
354 if canDoSearchSystem32() {
355 const LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
356 flags = LOAD_LIBRARY_SEARCH_SYSTEM32
357 } else if isBaseName(name) {
358 // WindowsXP or unpatched Windows machine
359 // trying to load "foo.dll" out of the system
360 // folder, but LoadLibraryEx doesn't support
361 // that yet on their system, so emulate it.
362 windir, _ := Getenv("WINDIR") // old var; apparently works on XP
364 return nil, errString("%WINDIR% not defined")
366 loadDLL = windir + "\\System32\\" + name
369 h, err := LoadLibraryEx(loadDLL, 0, flags)
373 return &DLL{Name: name, Handle: h}, nil
376 type errString string
378 func (s errString) Error() string { return string(s) }