key.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2015 The Go 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. // +build windows
  5. // Package registry provides access to the Windows registry.
  6. //
  7. // Here is a simple example, opening a registry key and reading a string value from it.
  8. //
  9. // k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
  10. // if err != nil {
  11. // log.Fatal(err)
  12. // }
  13. // defer k.Close()
  14. //
  15. // s, _, err := k.GetStringValue("SystemRoot")
  16. // if err != nil {
  17. // log.Fatal(err)
  18. // }
  19. // fmt.Printf("Windows system root is %q\n", s)
  20. //
  21. package registry
  22. import (
  23. "io"
  24. "syscall"
  25. "time"
  26. )
  27. const (
  28. // Registry key security and access rights.
  29. // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724878.aspx
  30. // for details.
  31. ALL_ACCESS = 0xf003f
  32. CREATE_LINK = 0x00020
  33. CREATE_SUB_KEY = 0x00004
  34. ENUMERATE_SUB_KEYS = 0x00008
  35. EXECUTE = 0x20019
  36. NOTIFY = 0x00010
  37. QUERY_VALUE = 0x00001
  38. READ = 0x20019
  39. SET_VALUE = 0x00002
  40. WOW64_32KEY = 0x00200
  41. WOW64_64KEY = 0x00100
  42. WRITE = 0x20006
  43. )
  44. // Key is a handle to an open Windows registry key.
  45. // Keys can be obtained by calling OpenKey; there are
  46. // also some predefined root keys such as CURRENT_USER.
  47. // Keys can be used directly in the Windows API.
  48. type Key syscall.Handle
  49. const (
  50. // Windows defines some predefined root keys that are always open.
  51. // An application can use these keys as entry points to the registry.
  52. // Normally these keys are used in OpenKey to open new keys,
  53. // but they can also be used anywhere a Key is required.
  54. CLASSES_ROOT = Key(syscall.HKEY_CLASSES_ROOT)
  55. CURRENT_USER = Key(syscall.HKEY_CURRENT_USER)
  56. LOCAL_MACHINE = Key(syscall.HKEY_LOCAL_MACHINE)
  57. USERS = Key(syscall.HKEY_USERS)
  58. CURRENT_CONFIG = Key(syscall.HKEY_CURRENT_CONFIG)
  59. )
  60. // Close closes open key k.
  61. func (k Key) Close() error {
  62. return syscall.RegCloseKey(syscall.Handle(k))
  63. }
  64. // OpenKey opens a new key with path name relative to key k.
  65. // It accepts any open key, including CURRENT_USER and others,
  66. // and returns the new key and an error.
  67. // The access parameter specifies desired access rights to the
  68. // key to be opened.
  69. func OpenKey(k Key, path string, access uint32) (Key, error) {
  70. p, err := syscall.UTF16PtrFromString(path)
  71. if err != nil {
  72. return 0, err
  73. }
  74. var subkey syscall.Handle
  75. err = syscall.RegOpenKeyEx(syscall.Handle(k), p, 0, access, &subkey)
  76. if err != nil {
  77. return 0, err
  78. }
  79. return Key(subkey), nil
  80. }
  81. // ReadSubKeyNames returns the names of subkeys of key k.
  82. // The parameter n controls the number of returned names,
  83. // analogous to the way os.File.Readdirnames works.
  84. func (k Key) ReadSubKeyNames(n int) ([]string, error) {
  85. ki, err := k.Stat()
  86. if err != nil {
  87. return nil, err
  88. }
  89. names := make([]string, 0, ki.SubKeyCount)
  90. buf := make([]uint16, ki.MaxSubKeyLen+1) // extra room for terminating zero byte
  91. loopItems:
  92. for i := uint32(0); ; i++ {
  93. if n > 0 {
  94. if len(names) == n {
  95. return names, nil
  96. }
  97. }
  98. l := uint32(len(buf))
  99. for {
  100. err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
  101. if err == nil {
  102. break
  103. }
  104. if err == syscall.ERROR_MORE_DATA {
  105. // Double buffer size and try again.
  106. l = uint32(2 * len(buf))
  107. buf = make([]uint16, l)
  108. continue
  109. }
  110. if err == _ERROR_NO_MORE_ITEMS {
  111. break loopItems
  112. }
  113. return names, err
  114. }
  115. names = append(names, syscall.UTF16ToString(buf[:l]))
  116. }
  117. if n > len(names) {
  118. return names, io.EOF
  119. }
  120. return names, nil
  121. }
  122. // CreateKey creates a key named path under open key k.
  123. // CreateKey returns the new key and a boolean flag that reports
  124. // whether the key already existed.
  125. // The access parameter specifies the access rights for the key
  126. // to be created.
  127. func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) {
  128. var h syscall.Handle
  129. var d uint32
  130. err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path),
  131. 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d)
  132. if err != nil {
  133. return 0, false, err
  134. }
  135. return Key(h), d == _REG_OPENED_EXISTING_KEY, nil
  136. }
  137. // DeleteKey deletes the subkey path of key k and its values.
  138. func DeleteKey(k Key, path string) error {
  139. return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path))
  140. }
  141. // A KeyInfo describes the statistics of a key. It is returned by Stat.
  142. type KeyInfo struct {
  143. SubKeyCount uint32
  144. MaxSubKeyLen uint32 // size of the key's subkey with the longest name, in Unicode characters, not including the terminating zero byte
  145. ValueCount uint32
  146. MaxValueNameLen uint32 // size of the key's longest value name, in Unicode characters, not including the terminating zero byte
  147. MaxValueLen uint32 // longest data component among the key's values, in bytes
  148. lastWriteTime syscall.Filetime
  149. }
  150. // ModTime returns the key's last write time.
  151. func (ki *KeyInfo) ModTime() time.Time {
  152. return time.Unix(0, ki.lastWriteTime.Nanoseconds())
  153. }
  154. // Stat retrieves information about the open key k.
  155. func (k Key) Stat() (*KeyInfo, error) {
  156. var ki KeyInfo
  157. err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil,
  158. &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount,
  159. &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime)
  160. if err != nil {
  161. return nil, err
  162. }
  163. return &ki, nil
  164. }