-
Notifications
You must be signed in to change notification settings - Fork 97
/
software.go
executable file
·220 lines (190 loc) · 5.39 KB
/
software.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//go:build windows && amd64
// +build windows,amd64
package winapi
import (
"fmt"
"time"
"golang.org/x/sys/windows/registry"
so "github.com/iamacarpet/go-win64api/shared"
)
func InstalledSoftwareList() ([]so.Software, error) {
sw64, err := getSoftwareList(`SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`, "", "X64")
if err != nil {
return nil, err
}
sw32, err := getSoftwareList(`SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall`, "", "X32")
if err != nil {
return nil, err
}
k, err := registry.OpenKey(registry.USERS, "", registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS)
if err != nil {
return nil, err
}
defer k.Close()
osUsers, err := k.ReadSubKeyNames(-1)
if err != nil {
return nil, err
}
swmap := make(map[string]bool)
var swList []so.Software
for _, sw := range sw64 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
for _, sw := range sw32 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
for _, osUser := range osUsers {
userSoftwareList64, err := getSoftwareList(`SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`, osUser, "X64")
if err == nil {
for _, sw := range userSoftwareList64 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
}
userSoftwareList32, err := getSoftwareList(`SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall`, osUser, "X32")
if err == nil {
for _, sw := range userSoftwareList32 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
}
userDataSoftwareList64, err := getSoftwareList(`SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData`, osUser, "X64")
if err == nil {
for _, sw := range userDataSoftwareList64 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
}
userDataSoftwareList32, err := getSoftwareList(`SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Installer\UserData`, osUser, "X32")
if err == nil {
for _, sw := range userDataSoftwareList32 {
if !swmap[sw.DisplayName] {
swList = append(swList, sw)
swmap[sw.DisplayName] = true
}
}
}
}
return swList, nil
}
func parseSoftware(rootKey registry.Key, path string) (so.Software, error) {
sk, err := registry.OpenKey(rootKey, path, registry.QUERY_VALUE)
if err != nil {
return so.Software{}, fmt.Errorf("Error reading from registry `%s`: %s", path, err.Error())
}
defer sk.Close()
dn, _, err := sk.GetStringValue("DisplayName")
if err != nil {
return so.Software{}, err
}
swv := so.Software{DisplayName: dn}
if rootKey == registry.LOCAL_MACHINE {
swv.RegKey = fmt.Sprintf(`HKLM\%s`, path)
} else if rootKey == registry.USERS {
swv.RegKey = fmt.Sprintf(`HKU\%s`, path)
}
dv, _, err := sk.GetStringValue("DisplayVersion")
if err == nil {
swv.DisplayVersion = dv
}
pub, _, err := sk.GetStringValue("Publisher")
if err == nil {
swv.Publisher = pub
}
id, _, err := sk.GetStringValue("InstallDate")
if err == nil {
swv.InstallDate, _ = time.Parse("20060102", id)
}
es, _, err := sk.GetIntegerValue("EstimatedSize")
if err == nil {
swv.EstimatedSize = es
}
cont, _, err := sk.GetStringValue("Contact")
if err == nil {
swv.Contact = cont
}
hlp, _, err := sk.GetStringValue("HelpLink")
if err == nil {
swv.HelpLink = hlp
}
isource, _, err := sk.GetStringValue("InstallSource")
if err == nil {
swv.InstallSource = isource
}
ilocaction, _, err := sk.GetStringValue("InstallLocation")
if err == nil {
swv.InstallLocation = ilocaction
}
ustring, _, err := sk.GetStringValue("UninstallString")
if err == nil {
swv.UninstallString = ustring
}
mver, _, err := sk.GetIntegerValue("VersionMajor")
if err == nil {
swv.VersionMajor = mver
}
mnver, _, err := sk.GetIntegerValue("VersionMinor")
if err == nil {
swv.VersionMinor = mnver
}
return swv, nil
}
func getSoftwareList(baseKey, user, arch string) ([]so.Software, error) {
rootKey := registry.LOCAL_MACHINE
if user != "" {
rootKey = registry.USERS
baseKey = user + `\` + baseKey
}
k, err := registry.OpenKey(rootKey, baseKey, registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS)
if err != nil {
return nil, fmt.Errorf("Error reading from registry: %s", err.Error())
}
defer k.Close()
swList := make([]so.Software, 0)
subkeys, err := k.ReadSubKeyNames(-1)
if err != nil {
return nil, fmt.Errorf("Error reading subkey list from registry: %s", err.Error())
}
for _, sw := range subkeys {
parsed, err := parseSoftware(rootKey, baseKey+`\`+sw)
if err != nil {
continue
}
parsed.Arch = arch
swList = append(swList, parsed)
}
return swList, nil
}
func getUserDataSoftwareList(baseKey, user, arch string) ([]so.Software, error) {
k, err := registry.OpenKey(registry.USERS, baseKey, registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS)
if err != nil {
return nil, fmt.Errorf("Error reading from registry: %s", err.Error())
}
defer k.Close()
swList := make([]so.Software, 0)
subkeys, err := k.ReadSubKeyNames(-1)
if err != nil {
return nil, fmt.Errorf("Error reading subkey list from registry: %s", err.Error())
}
for _, sw := range subkeys {
parsed, err := parseSoftware(registry.LOCAL_MACHINE, baseKey+`\`+sw)
if err != nil {
continue
}
parsed.Arch = arch
swList = append(swList, parsed)
}
return swList, nil
}