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)) + ".")
165 // A LazyDLL implements access to a single DLL.
166 // It will delay the load of the DLL until the first
167 // call to its Handle method or to one of its
168 // LazyProc's Addr method.
169 type LazyDLL struct {
172 // System determines whether the DLL must be loaded from the
173 // Windows System directory, bypassing the normal DLL search
178 dll *DLL // non nil once DLL is loaded
181 // Load loads DLL file d.Name into memory. It returns an error if fails.
182 // Load will not try to load DLL, if it is already loaded into memory.
183 func (d *LazyDLL) Load() error {
184 // Non-racy version of:
186 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
195 // kernel32.dll is special, since it's where LoadLibraryEx comes from.
196 // The kernel already special-cases its name, so it's always
197 // loaded from system32.
200 if d.Name == "kernel32.dll" {
201 dll, err = LoadDLL(d.Name)
203 dll, err = loadLibraryEx(d.Name, d.System)
209 // Non-racy version of:
211 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
215 // mustLoad is like Load but panics if search fails.
216 func (d *LazyDLL) mustLoad() {
223 // Handle returns d's module handle.
224 func (d *LazyDLL) Handle() uintptr {
226 return uintptr(d.dll.Handle)
229 // NewProc returns a LazyProc for accessing the named procedure in the DLL d.
230 func (d *LazyDLL) NewProc(name string) *LazyProc {
231 return &LazyProc{l: d, Name: name}
234 // NewLazyDLL creates new LazyDLL associated with DLL file.
235 func NewLazyDLL(name string) *LazyDLL {
236 return &LazyDLL{Name: name}
239 // NewLazySystemDLL is like NewLazyDLL, but will only
240 // search Windows System directory for the DLL if name is
241 // a base name (like "advapi32.dll").
242 func NewLazySystemDLL(name string) *LazyDLL {
243 return &LazyDLL{Name: name, System: true}
246 // A LazyProc implements access to a procedure inside a LazyDLL.
247 // It delays the lookup until the Addr method is called.
248 type LazyProc struct {
256 // Find searches DLL for procedure named p.Name. It returns
257 // an error if search fails. Find will not search procedure,
258 // if it is already found and loaded into memory.
259 func (p *LazyProc) Find() error {
260 // Non-racy version of:
261 // if p.proc == nil {
262 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
270 proc, e := p.l.dll.FindProc(p.Name)
274 // Non-racy version of:
276 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
282 // mustFind is like Find but panics if search fails.
283 func (p *LazyProc) mustFind() {
290 // Addr returns the address of the procedure represented by p.
291 // The return value can be passed to Syscall to run the procedure.
292 func (p *LazyProc) Addr() uintptr {
299 // Call executes procedure p with arguments a. It will panic, if more then 15 arguments
302 // The returned error is always non-nil, constructed from the result of GetLastError.
303 // Callers must inspect the primary return value to decide whether an error occurred
304 // (according to the semantics of the specific function being called) before consulting
305 // the error. The error will be guaranteed to contain windows.Errno.
306 func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
308 return p.proc.Call(a...)
311 var canDoSearchSystem32Once struct {
316 func initCanDoSearchSystem32() {
317 // https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
318 // "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
319 // Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
320 // systems that have KB2533623 installed. To determine whether the
321 // flags are available, use GetProcAddress to get the address of the
322 // AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
323 // function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
324 // flags can be used with LoadLibraryEx."
325 canDoSearchSystem32Once.v = (modkernel32.NewProc("AddDllDirectory").Find() == nil)
328 func canDoSearchSystem32() bool {
329 canDoSearchSystem32Once.Do(initCanDoSearchSystem32)
330 return canDoSearchSystem32Once.v
333 func isBaseName(name string) bool {
334 for _, c := range name {
335 if c == ':' || c == '/' || c == '\\' {
342 // loadLibraryEx wraps the Windows LoadLibraryEx function.
344 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
346 // If name is not an absolute path, LoadLibraryEx searches for the DLL
347 // in a variety of automatic locations unless constrained by flags.
348 // See: https://msdn.microsoft.com/en-us/library/ff919712%28VS.85%29.aspx
349 func loadLibraryEx(name string, system bool) (*DLL, error) {
353 if canDoSearchSystem32() {
354 const LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
355 flags = LOAD_LIBRARY_SEARCH_SYSTEM32
356 } else if isBaseName(name) {
357 // WindowsXP or unpatched Windows machine
358 // trying to load "foo.dll" out of the system
359 // folder, but LoadLibraryEx doesn't support
360 // that yet on their system, so emulate it.
361 windir, _ := Getenv("WINDIR") // old var; apparently works on XP
363 return nil, errString("%WINDIR% not defined")
365 loadDLL = windir + "\\System32\\" + name
368 h, err := LoadLibraryEx(loadDLL, 0, flags)
372 return &DLL{Name: name, Handle: h}, nil
375 type errString string
377 func (s errString) Error() string { return string(s) }