github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/parsers/kernel/kernel_windows.go (about)

     1  // +build windows
     2  
     3  package kernel
     4  
     5  import (
     6  	"fmt"
     7  	"unsafe"
     8  
     9  	"golang.org/x/sys/windows"
    10  )
    11  
    12  // VersionInfo holds information about the kernel.
    13  type VersionInfo struct {
    14  	kvi   string // Version of the kernel (e.g. 6.1.7601.17592 -> 6)
    15  	major int    // Major part of the kernel version (e.g. 6.1.7601.17592 -> 1)
    16  	minor int    // Minor part of the kernel version (e.g. 6.1.7601.17592 -> 7601)
    17  	build int    // Build number of the kernel version (e.g. 6.1.7601.17592 -> 17592)
    18  }
    19  
    20  func (k *VersionInfo) String() string {
    21  	return fmt.Sprintf("%d.%d %d (%s)", k.major, k.minor, k.build, k.kvi)
    22  }
    23  
    24  // GetKernelVersion gets the current kernel version.
    25  func GetKernelVersion() (*VersionInfo, error) {
    26  
    27  	var (
    28  		h         windows.Handle
    29  		dwVersion uint32
    30  		err       error
    31  	)
    32  
    33  	KVI := &VersionInfo{"Unknown", 0, 0, 0}
    34  
    35  	if err = windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE,
    36  		windows.StringToUTF16Ptr(`SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\`),
    37  		0,
    38  		windows.KEY_READ,
    39  		&h); err != nil {
    40  		return KVI, err
    41  	}
    42  	defer windows.RegCloseKey(h)
    43  
    44  	var buf [1 << 10]uint16
    45  	var typ uint32
    46  	n := uint32(len(buf) * 2) // api expects array of bytes, not uint16
    47  
    48  	if err = windows.RegQueryValueEx(h,
    49  		windows.StringToUTF16Ptr("BuildLabEx"),
    50  		nil,
    51  		&typ,
    52  		(*byte)(unsafe.Pointer(&buf[0])),
    53  		&n); err != nil {
    54  		return KVI, err
    55  	}
    56  
    57  	KVI.kvi = windows.UTF16ToString(buf[:])
    58  
    59  	// Important - docker.exe MUST be manifested for this API to return
    60  	// the correct information.
    61  	if dwVersion, err = windows.GetVersion(); err != nil {
    62  		return KVI, err
    63  	}
    64  
    65  	KVI.major = int(dwVersion & 0xFF)
    66  	KVI.minor = int((dwVersion & 0XFF00) >> 8)
    67  	KVI.build = int((dwVersion & 0xFFFF0000) >> 16)
    68  
    69  	return KVI, nil
    70  }