1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package registry
23
24 import (
25 "io"
26 "runtime"
27 "syscall"
28 "time"
29 )
30
31 const (
32
33
34
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
50
51
52
53 type Key syscall.Handle
54
55 const (
56
57
58
59
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 PERFORMANCE_DATA = Key(syscall.HKEY_PERFORMANCE_DATA)
66 )
67
68
69 func (k Key) Close() error {
70 return syscall.RegCloseKey(syscall.Handle(k))
71 }
72
73
74
75
76
77
78 func OpenKey(k Key, path string, access uint32) (Key, error) {
79 p, err := syscall.UTF16PtrFromString(path)
80 if err != nil {
81 return 0, err
82 }
83 var subkey syscall.Handle
84 err = syscall.RegOpenKeyEx(syscall.Handle(k), p, 0, access, &subkey)
85 if err != nil {
86 return 0, err
87 }
88 return Key(subkey), nil
89 }
90
91
92
93
94
95 func OpenRemoteKey(pcname string, k Key) (Key, error) {
96 var err error
97 var p *uint16
98 if pcname != "" {
99 p, err = syscall.UTF16PtrFromString(`\\` + pcname)
100 if err != nil {
101 return 0, err
102 }
103 }
104 var remoteKey syscall.Handle
105 err = regConnectRegistry(p, syscall.Handle(k), &remoteKey)
106 if err != nil {
107 return 0, err
108 }
109 return Key(remoteKey), nil
110 }
111
112
113
114
115 func (k Key) ReadSubKeyNames(n int) ([]string, error) {
116
117
118
119
120 runtime.LockOSThread()
121 defer runtime.UnlockOSThread()
122
123 names := make([]string, 0)
124
125
126 buf := make([]uint16, 256)
127 loopItems:
128 for i := uint32(0); ; i++ {
129 if n > 0 {
130 if len(names) == n {
131 return names, nil
132 }
133 }
134 l := uint32(len(buf))
135 for {
136 err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
137 if err == nil {
138 break
139 }
140 if err == syscall.ERROR_MORE_DATA {
141
142 l = uint32(2 * len(buf))
143 buf = make([]uint16, l)
144 continue
145 }
146 if err == _ERROR_NO_MORE_ITEMS {
147 break loopItems
148 }
149 return names, err
150 }
151 names = append(names, syscall.UTF16ToString(buf[:l]))
152 }
153 if n > len(names) {
154 return names, io.EOF
155 }
156 return names, nil
157 }
158
159
160
161
162
163
164 func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) {
165 var h syscall.Handle
166 var d uint32
167 err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path),
168 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d)
169 if err != nil {
170 return 0, false, err
171 }
172 return Key(h), d == _REG_OPENED_EXISTING_KEY, nil
173 }
174
175
176 func DeleteKey(k Key, path string) error {
177 return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path))
178 }
179
180
181 type KeyInfo struct {
182 SubKeyCount uint32
183 MaxSubKeyLen uint32
184 ValueCount uint32
185 MaxValueNameLen uint32
186 MaxValueLen uint32
187 lastWriteTime syscall.Filetime
188 }
189
190
191 func (ki *KeyInfo) ModTime() time.Time {
192 return time.Unix(0, ki.lastWriteTime.Nanoseconds())
193 }
194
195
196 func (k Key) Stat() (*KeyInfo, error) {
197 var ki KeyInfo
198 err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil,
199 &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount,
200 &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime)
201 if err != nil {
202 return nil, err
203 }
204 return &ki, nil
205 }
206
View as plain text