-
Notifications
You must be signed in to change notification settings - Fork 2
/
reg.go
178 lines (159 loc) · 4.53 KB
/
reg.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Package reg implements a simplified interface to the Windows Registry.
//
// A registry key is obtained by opening it, and each subsequent call will result in corresponding
// requests to the registry.
package reg
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"syscall"
"unsafe"
)
type RegKey syscall.Handle
var (
regEnumValueP, regSetValueExP *syscall.Proc
)
func init() {
dll := syscall.MustLoadDLL("advapi32.dll")
regEnumValueP = dll.MustFindProc("RegEnumValueW")
regSetValueExP = dll.MustFindProc("RegSetValueExW")
}
func OpenRegKey(path string, root RegKey) (RegKey, error) {
var key syscall.Handle
ps, _ := syscall.UTF16PtrFromString(path)
if err := syscall.RegOpenKeyEx(syscall.Handle(root), ps, 0, syscall.KEY_READ|syscall.KEY_SET_VALUE, &key); err != nil {
//if err := syscall.RegOpenKeyEx(syscall.Handle(root), ps, 0, syscall.KEY_READ, &key); err != nil {
return RegKey(0), err
}
return RegKey(key), nil
}
func (k RegKey) Close() {
syscall.RegCloseKey(syscall.Handle(k))
}
func (k RegKey) SubKeys() []string {
var nkeys, nvals uint32
if err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil, &nkeys, nil, nil, &nvals, nil, nil, nil, nil); err != nil {
panic(err)
}
var subkeys []string
var buf [1 << 10]uint16
for i := uint32(0); i < nkeys; i++ {
blen := uint32(len(buf))
if err := syscall.RegEnumKeyEx(syscall.Handle(k), i, &buf[0], &blen, nil, nil, nil, nil); err != nil {
panic(err)
}
k := syscall.UTF16ToString(buf[:])
subkeys = append(subkeys, k)
}
return subkeys
}
func (k RegKey) DWordValue(key string) (uint32, error) {
d, typ, err := k.regValue(key)
if err != nil {
return 0, err
}
if typ != syscall.REG_DWORD {
return 0, fmt.Errorf("Registry key not a DWORD")
}
var val uint32
buf := bytes.NewReader(d)
if err := binary.Read(buf, binary.LittleEndian, &val); err != nil {
return 0, err
}
return val, nil
}
func (k RegKey) SetDWordValue(name string, val uint32) error {
uname, _ := syscall.UTF16PtrFromString(name)
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, val); err != nil {
return err
}
if ret, _, _ := regSetValueExP.Call(
uintptr(k),
uintptr(unsafe.Pointer(uname)),
0,
uintptr(syscall.REG_DWORD),
uintptr(unsafe.Pointer(&buf.Bytes()[0])),
uintptr(buf.Len()),
); ret != 0 {
return fmt.Errorf("SetDWordValue error: %d", ret)
}
return nil
}
func (k RegKey) BoolValue(key string) (bool, error) {
d, t, err := k.regValue(key)
if err != nil {
return false, err
}
if t != syscall.REG_DWORD {
return false, fmt.Errorf("Registry key not a DWORD")
}
val, n := binary.Uvarint(d)
if n < 0 || val > 1 {
return false, fmt.Errorf("Value not a bool")
}
return val == 1, nil
}
func (k RegKey) StringValue(key string) (string, error) {
d, t, err := k.regValue(key)
if err != nil {
return "", err
}
if t != syscall.REG_SZ {
return "", fmt.Errorf("Registry key not a string")
}
buf := (*[1 << 10]uint16)(unsafe.Pointer(&d[0]))[:]
return syscall.UTF16ToString(buf), nil
}
func (k RegKey) Values() map[string]string {
var nkeys, nvals uint32
if err := syscall.RegQueryInfoKey(syscall.Handle(k), nil, nil, nil, &nkeys, nil, nil, &nvals, nil, nil, nil, nil); err != nil {
log.Fatal(err)
}
var values = make(map[string]string, nvals)
var buf [1 << 10]uint16
var data [1 << 10]byte
for i := uint32(0); i < nvals; i++ {
var typ uint32
blen := uint32(len(buf))
dlen := uint32(len(data))
if err := myRegEnumValue(syscall.Handle(k), i, &buf[0], &blen, &typ, &data[0], &dlen); err != nil {
panic(err)
}
valName := syscall.UTF16ToString(buf[:blen])
switch typ {
case syscall.REG_SZ:
values[valName] = "string"
case syscall.REG_DWORD:
values[valName] = "uint32"
case syscall.REG_BINARY:
values[valName] = "binary"
}
}
return values
}
func (k RegKey) regValue(key string) ([]byte, uint32, error) {
kname, _ := syscall.UTF16PtrFromString(key)
var typ uint32
var data [1 << 10]byte
dlen := uint32(len(data))
if err := syscall.RegQueryValueEx(syscall.Handle(k), kname, nil, &typ, &data[0], &dlen); err != nil {
return nil, 0, err
}
return data[:dlen], typ, nil
}
func myRegEnumValue(hKey syscall.Handle, index uint32, lpValueName *uint16, lpcchValueName *uint32, lpType *uint32, lpData *byte, lpcbData *uint32) error {
ret, _, err := regEnumValueP.Call(uintptr(hKey), uintptr(index),
uintptr(unsafe.Pointer(lpValueName)),
uintptr(unsafe.Pointer(lpcchValueName)),
0,
uintptr(unsafe.Pointer(lpType)),
uintptr(unsafe.Pointer(lpData)),
uintptr(unsafe.Pointer(lpcbData)))
if ret != 0 {
return err
}
return nil
}