-
Notifications
You must be signed in to change notification settings - Fork 885
/
registry.go
105 lines (82 loc) · 1.98 KB
/
registry.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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"syscall"
"unsafe"
)
import (
"github.com/lxn/win"
)
type RegistryKey struct {
hKey win.HKEY
}
func ClassesRootKey() *RegistryKey {
return &RegistryKey{win.HKEY_CLASSES_ROOT}
}
func CurrentUserKey() *RegistryKey {
return &RegistryKey{win.HKEY_CURRENT_USER}
}
func LocalMachineKey() *RegistryKey {
return &RegistryKey{win.HKEY_LOCAL_MACHINE}
}
func RegistryKeyString(rootKey *RegistryKey, subKeyPath, valueName string) (value string, err error) {
var hKey win.HKEY
if win.RegOpenKeyEx(
rootKey.hKey,
syscall.StringToUTF16Ptr(subKeyPath),
0,
win.KEY_READ,
&hKey) != win.ERROR_SUCCESS {
return "", newError("RegistryKeyString: Failed to open subkey.")
}
defer win.RegCloseKey(hKey)
var typ uint32
var data []uint16
var bufSize uint32
if win.ERROR_SUCCESS != win.RegQueryValueEx(
hKey,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
nil,
&bufSize) {
return "", newError("RegQueryValueEx #1")
}
data = make([]uint16, bufSize/2+1)
if win.ERROR_SUCCESS != win.RegQueryValueEx(
hKey,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
(*byte)(unsafe.Pointer(&data[0])),
&bufSize) {
return "", newError("RegQueryValueEx #2")
}
return syscall.UTF16ToString(data), nil
}
func RegistryKeyUint32(rootKey *RegistryKey, subKeyPath, valueName string) (value uint32, err error) {
var hKey win.HKEY
if win.RegOpenKeyEx(
rootKey.hKey,
syscall.StringToUTF16Ptr(subKeyPath),
0,
win.KEY_READ,
&hKey) != win.ERROR_SUCCESS {
return 0, newError("RegistryKeyUint32: Failed to open subkey.")
}
defer win.RegCloseKey(hKey)
bufSize := uint32(4)
if win.ERROR_SUCCESS != win.RegQueryValueEx(
hKey,
syscall.StringToUTF16Ptr(valueName),
nil,
nil,
(*byte)(unsafe.Pointer(&value)),
&bufSize) {
return 0, newError("RegQueryValueEx")
}
return
}