forked from iamacarpet/go-win64api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftware.go
executable file
·112 lines (89 loc) · 2.59 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
//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
}
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
}
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
}
swList = append(swList, swv)
}
sk.Close()
}
return swList, nil
}