From 3386a86fe557e9a021996be71a7f2287e4d1144e Mon Sep 17 00:00:00 2001 From: Aaron Klotz Date: Tue, 28 Sep 2021 16:29:47 -0600 Subject: [PATCH] util/winutil: add GetRegInteger This helper allows us to retrieve `DWORD` and `QWORD` values from the Tailscale key in the Windows registry. Signed-off-by: Aaron Klotz --- util/winutil/winutil.go | 23 +++++++++++++++++++++++ util/winutil/winutil_notwindows.go | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/util/winutil/winutil.go b/util/winutil/winutil.go index eacbd4866..81b3623a6 100644 --- a/util/winutil/winutil.go +++ b/util/winutil/winutil.go @@ -53,6 +53,29 @@ func GetRegString(name, defval string) string { return val } +// GetRegInteger looks up a registry path in our local machine path, or returns +// the given default if it can't. +// +// This function will only work on GOOS=windows. Trying to run it on any other +// OS will always return the default value. +func GetRegInteger(name string, defval uint64) uint64 { + key, err := registry.OpenKey(registry.LOCAL_MACHINE, RegBase, registry.READ) + if err != nil { + log.Printf("registry.OpenKey(%v): %v", RegBase, err) + return defval + } + defer key.Close() + + val, _, err := key.GetIntegerValue(name) + if err != nil { + if err != registry.ErrNotExist { + log.Printf("registry.GetIntegerValue(%v): %v", name, err) + } + return defval + } + return val +} + var ( kernel32 = syscall.NewLazyDLL("kernel32.dll") procWTSGetActiveConsoleSessionId = kernel32.NewProc("WTSGetActiveConsoleSessionId") diff --git a/util/winutil/winutil_notwindows.go b/util/winutil/winutil_notwindows.go index 0256df360..49a46e4ec 100644 --- a/util/winutil/winutil_notwindows.go +++ b/util/winutil/winutil_notwindows.go @@ -15,3 +15,10 @@ const RegBase = `` // This function will only work on GOOS=windows. Trying to run it on any other // OS will always return the default value. func GetRegString(name, defval string) string { return defval } + +// GetRegInteger looks up a registry path in our local machine path, or returns +// the given default if it can't. +// +// This function will only work on GOOS=windows. Trying to run it on any other +// OS will always return the default value. +func GetRegInteger(name string, defval uint64) uint64 { return defval }