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