winutil.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build windows
  5. // +build windows
  6. // Package winuntil contains misc Windows/win32 helper functions.
  7. package winutil
  8. import (
  9. "log"
  10. "syscall"
  11. "golang.org/x/sys/windows"
  12. "golang.org/x/sys/windows/registry"
  13. )
  14. const RegBase = `SOFTWARE\Tailscale IPN`
  15. // GetDesktopPID searches the PID of the process that's running the
  16. // currently active desktop and whether it was found.
  17. // Usually the PID will be for explorer.exe.
  18. func GetDesktopPID() (pid uint32, ok bool) {
  19. hwnd := windows.GetShellWindow()
  20. if hwnd == 0 {
  21. return 0, false
  22. }
  23. windows.GetWindowThreadProcessId(hwnd, &pid)
  24. return pid, pid != 0
  25. }
  26. // GetRegString looks up a registry path in our local machine path, or returns
  27. // the given default if it can't.
  28. //
  29. // This function will only work on GOOS=windows. Trying to run it on any other
  30. // OS will always return the default value.
  31. func GetRegString(name, defval string) string {
  32. key, err := registry.OpenKey(registry.LOCAL_MACHINE, RegBase, registry.READ)
  33. if err != nil {
  34. log.Printf("registry.OpenKey(%v): %v", RegBase, err)
  35. return defval
  36. }
  37. defer key.Close()
  38. val, _, err := key.GetStringValue(name)
  39. if err != nil {
  40. if err != registry.ErrNotExist {
  41. log.Printf("registry.GetStringValue(%v): %v", name, err)
  42. }
  43. return defval
  44. }
  45. return val
  46. }
  47. // GetRegInteger looks up a registry path in our local machine path, or returns
  48. // the given default if it can't.
  49. //
  50. // This function will only work on GOOS=windows. Trying to run it on any other
  51. // OS will always return the default value.
  52. func GetRegInteger(name string, defval uint64) uint64 {
  53. key, err := registry.OpenKey(registry.LOCAL_MACHINE, RegBase, registry.READ)
  54. if err != nil {
  55. log.Printf("registry.OpenKey(%v): %v", RegBase, err)
  56. return defval
  57. }
  58. defer key.Close()
  59. val, _, err := key.GetIntegerValue(name)
  60. if err != nil {
  61. if err != registry.ErrNotExist {
  62. log.Printf("registry.GetIntegerValue(%v): %v", name, err)
  63. }
  64. return defval
  65. }
  66. return val
  67. }
  68. var (
  69. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  70. procWTSGetActiveConsoleSessionId = kernel32.NewProc("WTSGetActiveConsoleSessionId")
  71. )
  72. // TODO(crawshaw): replace with x/sys/windows... one day.
  73. // https://go-review.googlesource.com/c/sys/+/331909
  74. func WTSGetActiveConsoleSessionId() uint32 {
  75. r1, _, _ := procWTSGetActiveConsoleSessionId.Call()
  76. return uint32(r1)
  77. }