Skip to content

Commit a9db919

Browse files
authored
Merge pull request #2 from viam-labs/find-win32-apps
Search Win32 hive for Win32 apps
2 parents 4dade5f + 036987d commit a9db919

1 file changed

Lines changed: 43 additions & 29 deletions

File tree

models/module.go

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -163,45 +163,59 @@ func (s *winRegSensorRegistry) Close(context.Context) error {
163163
}
164164

165165
func getWindowsProgramVersion(programName string) (string, error) {
166-
var subkey string
166+
if programName == "" {
167+
return "", fmt.Errorf("program name cannot be empty")
168+
}
169+
170+
for _, hive := range []string{
171+
// win64
172+
`SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`,
173+
//win32
174+
`SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`,
175+
} {
176+
version, err := searchRegistryHive(hive, programName)
177+
if err == nil {
178+
return version, nil
179+
}
180+
}
181+
182+
return "", fmt.Errorf("program '%s' not found or version information unavailable", programName)
183+
}
184+
185+
// searches for programName in HKLM/subkey, returns an error if not found.
186+
func searchRegistryHive(subkey, programName string) (string, error) {
187+
k, err := registry.OpenKey(registry.LOCAL_MACHINE, subkey, registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS)
188+
if err != nil {
189+
return "", err
190+
}
191+
defer k.Close()
167192

168-
// Attempt to find the program's uninstall information in the registry.
169-
if programName != "" {
170-
// subkey = `SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall` // Hive for Win32 applications
171-
subkey = `SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`
193+
subkeys, err := k.ReadSubKeyNames(-1)
194+
if err != nil {
195+
return "", err
196+
}
172197

173-
k, err := registry.OpenKey(registry.LOCAL_MACHINE, subkey, registry.QUERY_VALUE|registry.ENUMERATE_SUB_KEYS)
198+
for _, name := range subkeys {
199+
appKeyPath := filepath.Join(subkey, name)
200+
appKey, err := registry.OpenKey(registry.LOCAL_MACHINE, appKeyPath, registry.QUERY_VALUE)
174201
if err != nil {
175-
return "", err
202+
continue
176203
}
177-
defer k.Close()
204+
defer appKey.Close()
178205

179-
subkeys, err := k.ReadSubKeyNames(-1)
206+
displayName, _, err := appKey.GetStringValue("DisplayName")
180207
if err != nil {
181-
return "", err
208+
continue
182209
}
183210

184-
for _, name := range subkeys {
185-
appKeyPath := filepath.Join(subkey, name)
186-
appKey, err := registry.OpenKey(registry.LOCAL_MACHINE, appKeyPath, registry.QUERY_VALUE)
187-
if err != nil {
188-
continue
189-
}
190-
defer appKey.Close()
191-
192-
displayName, _, err := appKey.GetStringValue("DisplayName")
211+
if strings.Contains(displayName, programName) {
212+
version, _, err := appKey.GetStringValue("DisplayVersion")
193213
if err != nil {
194-
continue
195-
}
196-
197-
if strings.Contains(displayName, programName) {
198-
version, _, err := appKey.GetStringValue("DisplayVersion")
199-
if err != nil {
200-
return "", err
201-
}
202-
return version, nil
214+
return "", err
203215
}
216+
return version, nil
204217
}
205218
}
206-
return "", fmt.Errorf("program '%s' not found or version information unavailable", programName)
219+
220+
return "", fmt.Errorf("not found in %s", subkey)
207221
}

0 commit comments

Comments
 (0)