sramesh2k
asked on
Enumerate Registry keys
Hi! Experts
I want to enumerate the registry keys [under HKCU\Software] and add the keys to a List box in VB. I prefer using VBScript instead of API. How do I accomplish this using VBS and integrate with VB?
Thanks!
I want to enumerate the registry keys [under HKCU\Software] and add the keys to a List box in VB. I prefer using VBScript instead of API. How do I accomplish this using VBS and integrate with VB?
Thanks!
ASKER
That worked great! Related question, if you dont mind. Within this script, how do I set a certain criteria [example.., to list only the keys with the value "Installed" =1 in the right pane]
ASKER
And does it work with Windows 9x systems as well..?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, WMI itself have Installed software enumerator:
Private Sub Form_Load()
EnumerateInstalledSoftware ListView1
End Sub
Private Sub Form_Resize()
If WindowState = vbMinimized Then Exit Sub
ListView1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub EnumerateInstalledSoftware (lv As ListView)
lv.ListItems.Clear
lv.ColumnHeaders.Clear
lv.View = lvwReport
With lv.ColumnHeaders
.Add , , "Caption"
.Add , , "Description"
.Add , , "Identifying Number"
.Add , , "Install Date"
.Add , , "Install Location"
.Add , , "Install State"
.Add , , "Name"
.Add , , "Package Cache"
.Add , , "SKU Number"
.Add , , "Vendor"
.Add , , "Version"
End With
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery("S elect * from Win32_Product")
On Error Resume Next
For Each objSoftware In colSoftware
With lv.ListItems.Add(, , objSoftware.Caption)
.SubItems(1) = objSoftware.Description
.SubItems(2) = objSoftware.IdentifyingNum ber
.SubItems(3) = Str2Date(objSoftware.Insta llDate)
.SubItems(4) = objSoftware.InstallLocatio n
.SubItems(5) = GetInstallState(objSoftwar e.InstallS tate)
.SubItems(6) = objSoftware.Name
.SubItems(7) = objSoftware.PackageCache
.SubItems(8) = objSoftware.SKUNumber
.SubItems(9) = objSoftware.Vendor
.SubItems(10) = objSoftware.Version
End With
Next
End Sub
Private Function Str2Date(ByVal sDate As String) As Date
Str2Date = DateSerial(Left(sDate, 4), Mid(sDate, 5, 2), Mid(sDate, 7, 2))
End Function
Private Function GetInstallState(ByVal state As Integer) As String
Select Case state
Case 6: GetInstallState = "Bad Configuration"
Case -2: GetInstallState = "Invalid Argument"
Case -1: GetInstallState = "Unknown Package"
Case 1: GetInstallState = "Advertised"
Case 2: GetInstallState = "Absent"
Case 5: GetInstallState = "Installed"
End Select
End Function
Private Sub Form_Load()
EnumerateInstalledSoftware
End Sub
Private Sub Form_Resize()
If WindowState = vbMinimized Then Exit Sub
ListView1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub EnumerateInstalledSoftware
lv.ListItems.Clear
lv.ColumnHeaders.Clear
lv.View = lvwReport
With lv.ColumnHeaders
.Add , , "Caption"
.Add , , "Description"
.Add , , "Identifying Number"
.Add , , "Install Date"
.Add , , "Install Location"
.Add , , "Install State"
.Add , , "Name"
.Add , , "Package Cache"
.Add , , "SKU Number"
.Add , , "Vendor"
.Add , , "Version"
End With
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set colSoftware = objWMIService.ExecQuery("S
On Error Resume Next
For Each objSoftware In colSoftware
With lv.ListItems.Add(, , objSoftware.Caption)
.SubItems(1) = objSoftware.Description
.SubItems(2) = objSoftware.IdentifyingNum
.SubItems(3) = Str2Date(objSoftware.Insta
.SubItems(4) = objSoftware.InstallLocatio
.SubItems(5) = GetInstallState(objSoftwar
.SubItems(6) = objSoftware.Name
.SubItems(7) = objSoftware.PackageCache
.SubItems(8) = objSoftware.SKUNumber
.SubItems(9) = objSoftware.Vendor
.SubItems(10) = objSoftware.Version
End With
Next
End Sub
Private Function Str2Date(ByVal sDate As String) As Date
Str2Date = DateSerial(Left(sDate, 4), Mid(sDate, 5, 2), Mid(sDate, 7, 2))
End Function
Private Function GetInstallState(ByVal state As Integer) As String
Select Case state
Case 6: GetInstallState = "Bad Configuration"
Case -2: GetInstallState = "Invalid Argument"
Case -1: GetInstallState = "Unknown Package"
Case 1: GetInstallState = "Advertised"
Case 2: GetInstallState = "Absent"
Case 5: GetInstallState = "Installed"
End Select
End Function
ASKER
superb! :-)
Private Sub Command1_Click()
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:{imper
strKeyPath = "SOFTWARE"
objReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
List1.AddItem subkey
Next
End Sub