Source file src/internal/syscall/windows/registry/key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package registry
27
28 import (
29 "io"
30 "syscall"
31 )
32
33 const (
34
35
36
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
52
53
54
55 type Key syscall.Handle
56
57 const (
58
59
60
61
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
70 func (k Key) Close() error {
71 return syscall.RegCloseKey(syscall.Handle(k))
72 }
73
74
75
76
77
78
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
93
94
95 func (k Key) ReadSubKeyNames(n int) ([]string, error) {
96 names := make([]string, 0)
97
98
99 buf := make([]uint16, 256)
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
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
133
134
135
136
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
149 func DeleteKey(k Key, path string) error {
150 return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path))
151 }
152
153
154 type KeyInfo struct {
155 SubKeyCount uint32
156 MaxSubKeyLen uint32
157 ValueCount uint32
158 MaxValueNameLen uint32
159 MaxValueLen uint32
160 lastWriteTime syscall.Filetime
161 }
162
163
164 func (k Key) Stat() (*KeyInfo, error) {
165 var ki KeyInfo
166 err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil,
167 &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount,
168 &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime)
169 if err != nil {
170 return nil, err
171 }
172 return &ki, nil
173 }
174
View as plain text