github.com/SandwichDev/go-internals@v0.0.0-20210605002614-12311ac6b2c5/syscall/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 // NOTE: This package is a copy of golang.org/x/sys/windows/registry 24 // with KeyInfo.ModTime removed to prevent dependency cycles. 25 // 26 package registry 27 28 import "syscall" 29 30 const ( 31 // Registry key security and access rights. 32 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724878.aspx 33 // for details. 34 ALL_ACCESS = 0xf003f 35 CREATE_LINK = 0x00020 36 CREATE_SUB_KEY = 0x00004 37 ENUMERATE_SUB_KEYS = 0x00008 38 EXECUTE = 0x20019 39 NOTIFY = 0x00010 40 QUERY_VALUE = 0x00001 41 READ = 0x20019 42 SET_VALUE = 0x00002 43 WOW64_32KEY = 0x00200 44 WOW64_64KEY = 0x00100 45 WRITE = 0x20006 46 ) 47 48 // Key is a handle to an open Windows registry key. 49 // Keys can be obtained by calling OpenKey; there are 50 // also some predefined root keys such as CURRENT_USER. 51 // Keys can be used directly in the Windows API. 52 type Key syscall.Handle 53 54 const ( 55 // Windows defines some predefined root keys that are always open. 56 // An application can use these keys as entry points to the registry. 57 // Normally these keys are used in OpenKey to open new keys, 58 // but they can also be used anywhere a Key is required. 59 CLASSES_ROOT = Key(syscall.HKEY_CLASSES_ROOT) 60 CURRENT_USER = Key(syscall.HKEY_CURRENT_USER) 61 LOCAL_MACHINE = Key(syscall.HKEY_LOCAL_MACHINE) 62 USERS = Key(syscall.HKEY_USERS) 63 CURRENT_CONFIG = Key(syscall.HKEY_CURRENT_CONFIG) 64 ) 65 66 // Close closes open key k. 67 func (k Key) Close() error { 68 return syscall.RegCloseKey(syscall.Handle(k)) 69 } 70 71 // OpenKey opens a new key with path name relative to key k. 72 // It accepts any open key, including CURRENT_USER and others, 73 // and returns the new key and an error. 74 // The access parameter specifies desired access rights to the 75 // key to be opened. 76 func OpenKey(k Key, path string, access uint32) (Key, error) { 77 p, err := syscall.UTF16PtrFromString(path) 78 if err != nil { 79 return 0, err 80 } 81 var subkey syscall.Handle 82 err = syscall.RegOpenKeyEx(syscall.Handle(k), p, 0, access, &subkey) 83 if err != nil { 84 return 0, err 85 } 86 return Key(subkey), nil 87 } 88 89 // ReadSubKeyNames returns the names of subkeys of key k. 90 func (k Key) ReadSubKeyNames() ([]string, error) { 91 names := make([]string, 0) 92 // Registry key size limit is 255 bytes and described there: 93 // https://msdn.microsoft.com/library/windows/desktop/ms724872.aspx 94 buf := make([]uint16, 256) //plus extra room for terminating zero byte 95 loopItems: 96 for i := uint32(0); ; i++ { 97 l := uint32(len(buf)) 98 for { 99 err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil) 100 if err == nil { 101 break 102 } 103 if err == syscall.ERROR_MORE_DATA { 104 // Double buffer size and try again. 105 l = uint32(2 * len(buf)) 106 buf = make([]uint16, l) 107 continue 108 } 109 if err == _ERROR_NO_MORE_ITEMS { 110 break loopItems 111 } 112 return names, err 113 } 114 names = append(names, syscall.UTF16ToString(buf[:l])) 115 } 116 return names, nil 117 } 118 119 // CreateKey creates a key named path under open key k. 120 // CreateKey returns the new key and a boolean flag that reports 121 // whether the key already existed. 122 // The access parameter specifies the access rights for the key 123 // to be created. 124 func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) { 125 var h syscall.Handle 126 var d uint32 127 err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path), 128 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d) 129 if err != nil { 130 return 0, false, err 131 } 132 return Key(h), d == _REG_OPENED_EXISTING_KEY, nil 133 } 134 135 // DeleteKey deletes the subkey path of key k and its values. 136 func DeleteKey(k Key, path string) error { 137 return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path)) 138 } 139 140 // A KeyInfo describes the statistics of a key. It is returned by Stat. 141 type KeyInfo struct { 142 SubKeyCount uint32 143 MaxSubKeyLen uint32 // size of the key's subkey with the longest name, in Unicode characters, not including the terminating zero byte 144 ValueCount uint32 145 MaxValueNameLen uint32 // size of the key's longest value name, in Unicode characters, not including the terminating zero byte 146 MaxValueLen uint32 // longest data component among the key's values, in bytes 147 lastWriteTime syscall.Filetime 148 } 149 150 // Stat retrieves information about the open key k. 151 func (k Key) Stat() (*KeyInfo, error) { 152 var ki KeyInfo 153 err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil, 154 &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount, 155 &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime) 156 if err != nil { 157 return nil, err 158 } 159 return &ki, nil 160 }