2016-05-06 06:03:01 -04:00
|
|
|
// +build windows
|
|
|
|
|
2015-05-08 17:59:38 -04:00
|
|
|
package kernel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
2017-05-23 10:22:32 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2015-05-08 17:59:38 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// VersionInfo holds information about the kernel.
|
|
|
|
type VersionInfo struct {
|
|
|
|
kvi string // Version of the kernel (e.g. 6.1.7601.17592 -> 6)
|
|
|
|
major int // Major part of the kernel version (e.g. 6.1.7601.17592 -> 1)
|
|
|
|
minor int // Minor part of the kernel version (e.g. 6.1.7601.17592 -> 7601)
|
|
|
|
build int // Build number of the kernel version (e.g. 6.1.7601.17592 -> 17592)
|
2015-05-08 17:59:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
func (k *VersionInfo) String() string {
|
2015-05-08 17:59:38 -04:00
|
|
|
return fmt.Sprintf("%d.%d %d (%s)", k.major, k.minor, k.build, k.kvi)
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
2015-05-08 17:59:38 -04:00
|
|
|
|
|
|
|
var (
|
2017-05-23 10:22:32 -04:00
|
|
|
h windows.Handle
|
2015-05-08 17:59:38 -04:00
|
|
|
dwVersion uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
KVI := &VersionInfo{"Unknown", 0, 0, 0}
|
2015-05-08 17:59:38 -04:00
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err = windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE,
|
|
|
|
windows.StringToUTF16Ptr(`SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\`),
|
2015-05-08 17:59:38 -04:00
|
|
|
0,
|
2017-05-23 10:22:32 -04:00
|
|
|
windows.KEY_READ,
|
2015-05-08 17:59:38 -04:00
|
|
|
&h); err != nil {
|
|
|
|
return KVI, err
|
|
|
|
}
|
2017-05-23 10:22:32 -04:00
|
|
|
defer windows.RegCloseKey(h)
|
2015-05-08 17:59:38 -04:00
|
|
|
|
|
|
|
var buf [1 << 10]uint16
|
|
|
|
var typ uint32
|
|
|
|
n := uint32(len(buf) * 2) // api expects array of bytes, not uint16
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
if err = windows.RegQueryValueEx(h,
|
|
|
|
windows.StringToUTF16Ptr("BuildLabEx"),
|
2015-05-08 17:59:38 -04:00
|
|
|
nil,
|
|
|
|
&typ,
|
|
|
|
(*byte)(unsafe.Pointer(&buf[0])),
|
|
|
|
&n); err != nil {
|
|
|
|
return KVI, err
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
KVI.kvi = windows.UTF16ToString(buf[:])
|
2015-05-08 17:59:38 -04:00
|
|
|
|
|
|
|
// Important - docker.exe MUST be manifested for this API to return
|
|
|
|
// the correct information.
|
2017-05-23 10:22:32 -04:00
|
|
|
if dwVersion, err = windows.GetVersion(); err != nil {
|
2015-05-08 17:59:38 -04:00
|
|
|
return KVI, err
|
|
|
|
}
|
|
|
|
|
|
|
|
KVI.major = int(dwVersion & 0xFF)
|
|
|
|
KVI.minor = int((dwVersion & 0XFF00) >> 8)
|
|
|
|
KVI.build = int((dwVersion & 0xFFFF0000) >> 16)
|
|
|
|
|
|
|
|
return KVI, nil
|
|
|
|
}
|