forked from iamacarpet/go-win64api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
software.go
executable file
·57 lines (46 loc) · 1.47 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
// +build windows,amd64
package winapi
import (
"fmt"
"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
}
return append(sw64, sw32...), nil
}
func getSoftwareList(baseKey string, arch string) ([]so.Software, error) {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, 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 {
sk, err := registry.OpenKey(registry.LOCAL_MACHINE, baseKey+`\`+sw, registry.QUERY_VALUE)
if err != nil {
return nil, fmt.Errorf("Error reading from registry (subkey %s): %s", sw, err.Error())
}
dn, _, err := sk.GetStringValue("DisplayName")
if err == nil {
swv := so.Software{DisplayName: dn, Arch: arch}
dv, _, err := sk.GetStringValue("DisplayVersion")
if err == nil {
swv.DisplayVersion = dv
}
swList = append(swList, swv)
}
}
return swList, nil
}