5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 08:11:36 +00:00
matterbridge/vendor/github.com/rs/xid/hostid_windows.go

39 lines
1.0 KiB
Go
Raw Normal View History

2018-05-27 19:48:57 +00:00
// +build windows
package xid
import (
"fmt"
"syscall"
2018-05-27 19:48:57 +00:00
"unsafe"
)
func readPlatformMachineID() (string, error) {
// source: https://github.com/shirou/gopsutil/blob/master/host/host_syscall.go
var h syscall.Handle
err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, syscall.KEY_READ|syscall.KEY_WOW64_64KEY, &h)
2018-05-27 19:48:57 +00:00
if err != nil {
return "", err
}
defer syscall.RegCloseKey(h)
2018-05-27 19:48:57 +00:00
const syscallRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
2018-05-27 19:48:57 +00:00
const uuidLen = 36
var regBuf [syscallRegBufLen]uint16
bufLen := uint32(syscallRegBufLen)
2018-05-27 19:48:57 +00:00
var valType uint32
err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
2018-05-27 19:48:57 +00:00
if err != nil {
return "", err
}
hostID := syscall.UTF16ToString(regBuf[:])
2018-05-27 19:48:57 +00:00
hostIDLen := len(hostID)
if hostIDLen != uuidLen {
return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
}
return hostID, nil
}