Solved

Enumerate Registry keys

Posted on 2004-04-01
6
9,347 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:sramesh2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 10738058
Add command button and listbox on form:

Private Sub Command1_Click()
   Const HKEY_CURRENT_USER = &H80000001
   strComputer = "."
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
   strKeyPath = "SOFTWARE"
   objReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
   For Each subkey In arrSubKeys
       List1.AddItem subkey
   Next
End Sub
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 10738108
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]
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 10738128
And does it work with Windows 9x systems as well..?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Accepted Solution

by:
Ark earned 250 total points
ID: 10738265
Private Sub Command1_Click()
   Const HKEY_CURRENT_USER = &H80000001
   strComputer = "."
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
   strKeyPath = "SOFTWARE"
   objReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
   For Each subkey In arrSubKeys
       ret = objReg.GetStringValue(HKEY_CURRENT_USER, strKeyPath & "\Installed", sValue 'if 1 is a string
'Or   ret =  objReg.GetDWORDValue(HKEY_CURRENT_USER, strKeyPath & "\Installed", lValue if 1 is DWORD
       If ret<>0 Then
          List1.AddItem subkey & "Installed info - not found"
       Else
          List1.AddItem subkey & "Installed info - " & sValue
       End If
   Next
End Sub

PS
WMI instrumentation distributed by MS with w2000/Xp, but there is free redistributable package from MS for w9x
0
 
LVL 28

Expert Comment

by:Ark
ID: 10738290
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=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colSoftware = objWMIService.ExecQuery("Select * 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.IdentifyingNumber
            .SubItems(3) = Str2Date(objSoftware.InstallDate)
            .SubItems(4) = objSoftware.InstallLocation
            .SubItems(5) = GetInstallState(objSoftware.InstallState)
            .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
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 10738356
superb! :-)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Looking for VB6 code to read SQL table export it to ascii 8 61
VBA Shell can't Find Word document 11 155
Excel Automation VBA 19 116
SQL Server 2012 to SQL Server 2016 24 57
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question