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 "runtime"
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 func (k Key) ReadSubKeyNames() ([]string, error) {
94
95
96
97 runtime.LockOSThread()
98 defer runtime.UnlockOSThread()
99
100 names := make([]string, 0)
101
102
103 buf := make([]uint16, 256)
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
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
129
130
131
132
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
145 func DeleteKey(k Key, path string) error {
146 return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path))
147 }
148
149
150 type KeyInfo struct {
151 SubKeyCount uint32
152 MaxSubKeyLen uint32
153 ValueCount uint32
154 MaxValueNameLen uint32
155 MaxValueLen uint32
156 lastWriteTime syscall.Filetime
157 }
158
159
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 }
170
View as plain text