github.com/tailscale/wireguard-go@v0.0.20201119-0.20210522003738-46b531feb08a/tun/wintun/dll_fromfile_windows.go (about) 1 // +build !load_wintun_from_rsrc 2 3 /* SPDX-License-Identifier: MIT 4 * 5 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. 6 */ 7 8 package wintun 9 10 import ( 11 "fmt" 12 "sync" 13 "sync/atomic" 14 "unsafe" 15 16 "golang.org/x/sys/windows" 17 ) 18 19 type lazyDLL struct { 20 Name string 21 mu sync.Mutex 22 module windows.Handle 23 onLoad func(d *lazyDLL) 24 } 25 26 func (d *lazyDLL) Load() error { 27 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.module))) != nil { 28 return nil 29 } 30 d.mu.Lock() 31 defer d.mu.Unlock() 32 if d.module != 0 { 33 return nil 34 } 35 36 const ( 37 LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200 38 LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 39 ) 40 module, err := windows.LoadLibraryEx(d.Name, 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR|LOAD_LIBRARY_SEARCH_SYSTEM32) 41 if err != nil { 42 return fmt.Errorf("Unable to load library: %w", err) 43 } 44 45 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.module)), unsafe.Pointer(module)) 46 if d.onLoad != nil { 47 d.onLoad(d) 48 } 49 return nil 50 } 51 52 func (p *lazyProc) nameToAddr() (uintptr, error) { 53 return windows.GetProcAddress(p.dll.module, p.Name) 54 }