mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-28 13:48:49 +08:00
[host][windows] Refactor code to query registry via golang.org/x/sys/windows/registry
This commit is contained in:
parent
eb11fd6e2f
commit
c729bbd6a8
@ -15,7 +15,6 @@ import (
|
||||
|
||||
"github.com/shirou/gopsutil/internal/common"
|
||||
process "github.com/shirou/gopsutil/process"
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
@ -94,30 +93,15 @@ func InfoWithContext(ctx context.Context) (*InfoStat, error) {
|
||||
}
|
||||
|
||||
func getMachineGuid() (string, error) {
|
||||
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)
|
||||
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Cryptography`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
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)
|
||||
defer k.Close()
|
||||
hostID, _, err := k.GetStringValue("MachineGuid")
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user