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