[fix] embed_windows

This commit is contained in:
maze.y2b@gmail.com 2021-11-27 21:51:38 +08:00
parent 353ae30839
commit e4b30dacd4

View file

@ -11,9 +11,6 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"unsafe" "unsafe"
C "github.com/Dreamacro/clash/constant"
"golang.org/x/sys/windows"
) )
func newLazyDLL(name string, onLoad func(d *lazyDLL)) *lazyDLL { func newLazyDLL(name string, onLoad func(d *lazyDLL)) *lazyDLL {
@ -65,7 +62,7 @@ func (p *lazyProc) Addr() uintptr {
type lazyDLL struct { type lazyDLL struct {
Name string Name string
mu sync.Mutex mu sync.Mutex
module windows.Handle module *memmod.Module
onLoad func(d *lazyDLL) onLoad func(d *lazyDLL)
} }
@ -75,7 +72,7 @@ func (d *lazyDLL) Load() error {
} }
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock() defer d.mu.Unlock()
if d.module != 0 { if d.module != nil {
return nil return nil
} }
@ -92,35 +89,5 @@ func (d *lazyDLL) Load() error {
} }
func (p *lazyProc) nameToAddr() (uintptr, error) { func (p *lazyProc) nameToAddr() (uintptr, error) {
return windows.GetProcAddress(p.dll.module, p.Name) return p.dll.module.ProcAddressByName(p.Name)
}
// Version returns the version of the Wintun DLL.
func Version() string {
if modwintun.Load() != nil {
return "unknown"
}
resInfo, err := windows.FindResource(modwintun.module, windows.ResourceID(1), windows.RT_VERSION)
if err != nil {
return "unknown"
}
data, err := windows.LoadResourceData(modwintun.module, resInfo)
if err != nil {
return "unknown"
}
var fixedInfo *windows.VS_FIXEDFILEINFO
fixedInfoLen := uint32(unsafe.Sizeof(*fixedInfo))
err = windows.VerQueryValue(unsafe.Pointer(&data[0]), `\`, unsafe.Pointer(&fixedInfo), &fixedInfoLen)
if err != nil {
return "unknown"
}
version := fmt.Sprintf("%d.%d", (fixedInfo.FileVersionMS>>16)&0xff, (fixedInfo.FileVersionMS>>0)&0xff)
if nextNibble := (fixedInfo.FileVersionLS >> 16) & 0xff; nextNibble != 0 {
version += fmt.Sprintf(".%d", nextNibble)
}
if nextNibble := (fixedInfo.FileVersionLS >> 0) & 0xff; nextNibble != 0 {
version += fmt.Sprintf(".%d", nextNibble)
}
return version
} }