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 package registry
26
27 import (
28 "runtime"
29 "syscall"
30 )
31
32 const (
33
34
35
36 ALL_ACCESS = 0xf003f
37 CREATE_LINK = 0x00020
38 CREATE_SUB_KEY = 0x00004
39 ENUMERATE_SUB_KEYS = 0x00008
40 EXECUTE = 0x20019
41 NOTIFY = 0x00010
42 QUERY_VALUE = 0x00001
43 READ = 0x20019
44 SET_VALUE = 0x00002
45 WOW64_32KEY = 0x00200
46 WOW64_64KEY = 0x00100
47 WRITE = 0x20006
48 )
49
50
51
52
53
54 type Key syscall.Handle
55
56 const (
57
58
59
60
61 CLASSES_ROOT = Key(syscall.HKEY_CLASSES_ROOT)
62 CURRENT_USER = Key(syscall.HKEY_CURRENT_USER)
63 LOCAL_MACHINE = Key(syscall.HKEY_LOCAL_MACHINE)
64 USERS = Key(syscall.HKEY_USERS)
65 CURRENT_CONFIG = Key(syscall.HKEY_CURRENT_CONFIG)
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 func (k Key) ReadSubKeyNames() ([]string, error) {
93
94
95
96 runtime.LockOSThread()
97 defer runtime.UnlockOSThread()
98
99 names := make([]string, 0)
100
101
102 buf := make([]uint16, 256)
103 loopItems:
104 for i := uint32(0); ; i++ {
105 l := uint32(len(buf))
106 for {
107 err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &l, nil, nil, nil, nil)
108 if err == nil {
109 break
110 }
111 if err == syscall.ERROR_MORE_DATA {
112
113 l = uint32(2 * len(buf))
114 buf = make([]uint16, l)
115 continue
116 }
117 if err == _ERROR_NO_MORE_ITEMS {
118 break loopItems
119 }
120 return names, err
121 }
122 names = append(names, syscall.UTF16ToString(buf[:l]))
123 }
124 return names, nil
125 }
126
127
128
129
130
131
132 func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error) {
133 var h syscall.Handle
134 var d uint32
135 err = regCreateKeyEx(syscall.Handle(k), syscall.StringToUTF16Ptr(path),
136 0, nil, _REG_OPTION_NON_VOLATILE, access, nil, &h, &d)
137 if err != nil {
138 return 0, false, err
139 }
140 return Key(h), d == _REG_OPENED_EXISTING_KEY, nil
141 }
142
143
144 func DeleteKey(k Key, path string) error {
145 return regDeleteKey(syscall.Handle(k), syscall.StringToUTF16Ptr(path))
146 }
147
148
149 type KeyInfo struct {
150 SubKeyCount uint32
151 MaxSubKeyLen uint32
152 ValueCount uint32
153 MaxValueNameLen uint32
154 MaxValueLen uint32
155 lastWriteTime syscall.Filetime
156 }
157
158
159 func (k Key) Stat() (*KeyInfo, error) {
160 var ki KeyInfo
161 err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil,
162 &ki.SubKeyCount, &ki.MaxSubKeyLen, nil, &ki.ValueCount,
163 &ki.MaxValueNameLen, &ki.MaxValueLen, nil, &ki.lastWriteTime)
164 if err != nil {
165 return nil, err
166 }
167 return &ki, nil
168 }
169
View as plain text