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.
18 // Registry value types.
28 FULL_RESOURCE_DESCRIPTOR = 9
29 RESOURCE_REQUIREMENTS_LIST = 10
34 // ErrShortBuffer is returned when the buffer was too short for the operation.
35 ErrShortBuffer = syscall.ERROR_MORE_DATA
37 // ErrNotExist is returned when a registry key or value does not exist.
38 ErrNotExist = syscall.ERROR_FILE_NOT_FOUND
40 // ErrUnexpectedType is returned by Get*Value when the value's type was unexpected.
41 ErrUnexpectedType = errors.New("unexpected key value type")
44 // GetValue retrieves the type and data for the specified value associated
45 // with an open key k. It fills up buffer buf and returns the retrieved
46 // byte count n. If buf is too small to fit the stored value it returns
47 // ErrShortBuffer error along with the required buffer size n.
48 // If no buffer is provided, it returns true and actual buffer size n.
49 // If no buffer is provided, GetValue returns the value's type only.
50 // If the value does not exist, the error returned is ErrNotExist.
52 // GetValue is a low level function. If value's type is known, use the appropriate
53 // Get*Value function instead.
54 func (k Key) GetValue(name string, buf []byte) (n int, valtype uint32, err error) {
55 pname, err := syscall.UTF16PtrFromString(name)
61 pbuf = (*byte)(unsafe.Pointer(&buf[0]))
64 err = syscall.RegQueryValueEx(syscall.Handle(k), pname, nil, &valtype, pbuf, &l)
66 return int(l), valtype, err
68 return int(l), valtype, nil
71 func (k Key) getValue(name string, buf []byte) (date []byte, valtype uint32, err error) {
72 p, err := syscall.UTF16PtrFromString(name)
79 err = syscall.RegQueryValueEx(syscall.Handle(k), p, nil, &t, (*byte)(unsafe.Pointer(&buf[0])), &n)
81 return buf[:n], t, nil
83 if err != syscall.ERROR_MORE_DATA {
86 if n <= uint32(len(buf)) {
93 // GetStringValue retrieves the string value for the specified
94 // value name associated with an open key k. It also returns the value's type.
95 // If value does not exist, GetStringValue returns ErrNotExist.
96 // If value is not SZ or EXPAND_SZ, it will return the correct value
97 // type and ErrUnexpectedType.
98 func (k Key) GetStringValue(name string) (val string, valtype uint32, err error) {
99 data, typ, err2 := k.getValue(name, make([]byte, 64))
106 return "", typ, ErrUnexpectedType
111 u := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[:]
112 return syscall.UTF16ToString(u), typ, nil
115 // GetMUIStringValue retrieves the localized string value for
116 // the specified value name associated with an open key k.
117 // If the value name doesn't exist or the localized string value
118 // can't be resolved, GetMUIStringValue returns ErrNotExist.
119 // GetMUIStringValue panics if the system doesn't support
120 // regLoadMUIString; use LoadRegLoadMUIString to check if
121 // regLoadMUIString is supported before calling this function.
122 func (k Key) GetMUIStringValue(name string) (string, error) {
123 pname, err := syscall.UTF16PtrFromString(name)
128 buf := make([]uint16, 1024)
132 err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
133 if err == syscall.ERROR_FILE_NOT_FOUND { // Try fallback path
135 // Try to resolve the string value using the system directory as
136 // a DLL search path; this assumes the string value is of the form
137 // @[path]\dllname,-strID but with no path given, e.g. @tzres.dll,-320.
139 // This approach works with tzres.dll but may have to be revised
140 // in the future to allow callers to provide custom search paths.
143 s, err = ExpandString("%SystemRoot%\\system32\\")
147 pdir, err = syscall.UTF16PtrFromString(s)
152 err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
155 for err == syscall.ERROR_MORE_DATA { // Grow buffer if needed
156 if buflen <= uint32(len(buf)) {
157 break // Buffer not growing, assume race; break
159 buf = make([]uint16, buflen)
160 err = regLoadMUIString(syscall.Handle(k), pname, &buf[0], uint32(len(buf)), &buflen, 0, pdir)
167 return syscall.UTF16ToString(buf), nil
170 // ExpandString expands environment-variable strings and replaces
171 // them with the values defined for the current user.
172 // Use ExpandString to expand EXPAND_SZ strings.
173 func ExpandString(value string) (string, error) {
177 p, err := syscall.UTF16PtrFromString(value)
181 r := make([]uint16, 100)
183 n, err := expandEnvironmentStrings(p, &r[0], uint32(len(r)))
187 if n <= uint32(len(r)) {
188 u := (*[1 << 29]uint16)(unsafe.Pointer(&r[0]))[:]
189 return syscall.UTF16ToString(u), nil
191 r = make([]uint16, n)
195 // GetStringsValue retrieves the []string value for the specified
196 // value name associated with an open key k. It also returns the value's type.
197 // If value does not exist, GetStringsValue returns ErrNotExist.
198 // If value is not MULTI_SZ, it will return the correct value
199 // type and ErrUnexpectedType.
200 func (k Key) GetStringsValue(name string) (val []string, valtype uint32, err error) {
201 data, typ, err2 := k.getValue(name, make([]byte, 64))
203 return nil, typ, err2
206 return nil, typ, ErrUnexpectedType
211 p := (*[1 << 29]uint16)(unsafe.Pointer(&data[0]))[:len(data)/2]
215 if p[len(p)-1] == 0 {
216 p = p[:len(p)-1] // remove terminating null
218 val = make([]string, 0, 5)
220 for i, c := range p {
222 val = append(val, string(utf16.Decode(p[from:i])))
229 // GetIntegerValue retrieves the integer value for the specified
230 // value name associated with an open key k. It also returns the value's type.
231 // If value does not exist, GetIntegerValue returns ErrNotExist.
232 // If value is not DWORD or QWORD, it will return the correct value
233 // type and ErrUnexpectedType.
234 func (k Key) GetIntegerValue(name string) (val uint64, valtype uint32, err error) {
235 data, typ, err2 := k.getValue(name, make([]byte, 8))
242 return 0, typ, errors.New("DWORD value is not 4 bytes long")
244 return uint64(*(*uint32)(unsafe.Pointer(&data[0]))), DWORD, nil
247 return 0, typ, errors.New("QWORD value is not 8 bytes long")
249 return uint64(*(*uint64)(unsafe.Pointer(&data[0]))), QWORD, nil
251 return 0, typ, ErrUnexpectedType
255 // GetBinaryValue retrieves the binary value for the specified
256 // value name associated with an open key k. It also returns the value's type.
257 // If value does not exist, GetBinaryValue returns ErrNotExist.
258 // If value is not BINARY, it will return the correct value
259 // type and ErrUnexpectedType.
260 func (k Key) GetBinaryValue(name string) (val []byte, valtype uint32, err error) {
261 data, typ, err2 := k.getValue(name, make([]byte, 64))
263 return nil, typ, err2
266 return nil, typ, ErrUnexpectedType
268 return data, typ, nil
271 func (k Key) setValue(name string, valtype uint32, data []byte) error {
272 p, err := syscall.UTF16PtrFromString(name)
277 return regSetValueEx(syscall.Handle(k), p, 0, valtype, nil, 0)
279 return regSetValueEx(syscall.Handle(k), p, 0, valtype, &data[0], uint32(len(data)))
282 // SetDWordValue sets the data and type of a name value
283 // under key k to value and DWORD.
284 func (k Key) SetDWordValue(name string, value uint32) error {
285 return k.setValue(name, DWORD, (*[4]byte)(unsafe.Pointer(&value))[:])
288 // SetQWordValue sets the data and type of a name value
289 // under key k to value and QWORD.
290 func (k Key) SetQWordValue(name string, value uint64) error {
291 return k.setValue(name, QWORD, (*[8]byte)(unsafe.Pointer(&value))[:])
294 func (k Key) setStringValue(name string, valtype uint32, value string) error {
295 v, err := syscall.UTF16FromString(value)
299 buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[:len(v)*2]
300 return k.setValue(name, valtype, buf)
303 // SetStringValue sets the data and type of a name value
304 // under key k to value and SZ. The value must not contain a zero byte.
305 func (k Key) SetStringValue(name, value string) error {
306 return k.setStringValue(name, SZ, value)
309 // SetExpandStringValue sets the data and type of a name value
310 // under key k to value and EXPAND_SZ. The value must not contain a zero byte.
311 func (k Key) SetExpandStringValue(name, value string) error {
312 return k.setStringValue(name, EXPAND_SZ, value)
315 // SetStringsValue sets the data and type of a name value
316 // under key k to value and MULTI_SZ. The value strings
317 // must not contain a zero byte.
318 func (k Key) SetStringsValue(name string, value []string) error {
320 for _, s := range value {
321 for i := 0; i < len(s); i++ {
323 return errors.New("string cannot have 0 inside")
328 v := utf16.Encode([]rune(ss + "\x00"))
329 buf := (*[1 << 29]byte)(unsafe.Pointer(&v[0]))[:len(v)*2]
330 return k.setValue(name, MULTI_SZ, buf)
333 // SetBinaryValue sets the data and type of a name value
334 // under key k to value and BINARY.
335 func (k Key) SetBinaryValue(name string, value []byte) error {
336 return k.setValue(name, BINARY, value)
339 // DeleteValue removes a named value from the key k.
340 func (k Key) DeleteValue(name string) error {
341 return regDeleteValue(syscall.Handle(k), syscall.StringToUTF16Ptr(name))
344 // ReadValueNames returns the value names of key k.
345 // The parameter n controls the number of returned names,
346 // analogous to the way os.File.Readdirnames works.
347 func (k Key) ReadValueNames(n int) ([]string, error) {
352 names := make([]string, 0, ki.ValueCount)
353 buf := make([]uint16, ki.MaxValueNameLen+1) // extra room for terminating null character
355 for i := uint32(0); ; i++ {
361 l := uint32(len(buf))
363 err := regEnumValue(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
367 if err == syscall.ERROR_MORE_DATA {
368 // Double buffer size and try again.
369 l = uint32(2 * len(buf))
370 buf = make([]uint16, l)
373 if err == _ERROR_NO_MORE_ITEMS {
378 names = append(names, syscall.UTF16ToString(buf[:l]))