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