2023-11-04 19:51:35 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package winversion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-08-29 22:26:15 +00:00
|
|
|
"sync"
|
2023-11-04 19:51:35 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
)
|
|
|
|
|
2024-08-29 22:26:15 +00:00
|
|
|
var WindowsVersionFloat = sync.OnceValue[float64](func() float64 {
|
|
|
|
version, err := getWindowsVersion()
|
2023-11-04 19:51:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-08-29 22:26:15 +00:00
|
|
|
|
|
|
|
return version
|
|
|
|
})
|
2023-11-04 19:51:35 +00:00
|
|
|
|
|
|
|
// GetWindowsVersion reads the version number of the OS from the Registry
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version
|
2024-08-29 22:26:15 +00:00
|
|
|
func getWindowsVersion() (float64, error) {
|
2023-11-04 19:51:35 +00:00
|
|
|
reg, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
|
|
|
if err != nil {
|
2024-08-29 22:26:15 +00:00
|
|
|
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer reg.Close()
|
|
|
|
|
|
|
|
windowsVersion, _, err := reg.GetStringValue("CurrentVersion")
|
|
|
|
if err != nil {
|
2024-08-29 22:26:15 +00:00
|
|
|
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
windowsVersionFloat, err := strconv.ParseFloat(windowsVersion, 64)
|
|
|
|
if err != nil {
|
2024-08-29 22:26:15 +00:00
|
|
|
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-29 22:26:15 +00:00
|
|
|
return windowsVersionFloat, nil
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|