From d8a6a50129bd0ce6ab93c1a73f4be0ebd9b30b25 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Mon, 18 Jun 2018 22:17:31 +0200 Subject: [PATCH] Revert "[host][windows] Refactor code to query registry via golang.org/x/sys/windows/registry" This reverts commit c729bbd6a8e6fd243e2f43c24666c29feb3e6294. --- host/host_windows.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/host/host_windows.go b/host/host_windows.go index 1c65c5c..6fe6142 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -15,6 +15,7 @@ import ( "github.com/shirou/gopsutil/internal/common" process "github.com/shirou/gopsutil/process" + "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" ) @@ -93,15 +94,30 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) { } func getMachineGuid() (string, error) { - k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Cryptography`, registry.QUERY_VALUE) + var h windows.Handle + err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h) if err != nil { return "", err } - defer k.Close() - hostID, _, err := k.GetStringValue("MachineGuid") + defer windows.RegCloseKey(h) + + const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16 + const uuidLen = 36 + + var regBuf [windowsRegBufLen]uint16 + bufLen := uint32(windowsRegBufLen) + var valType uint32 + err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) if err != nil { return "", err } + + hostID := windows.UTF16ToString(regBuf[:]) + hostIDLen := len(hostID) + if hostIDLen != uuidLen { + return "", fmt.Errorf("HostID incorrect: %q\n", hostID) + } + return hostID, nil }