Retreiving Subkeys in the registry for VB6

I am new to Experts Exchange and I have a question about retreiving subkeys from the registry.  I need to be able to pull the subkeys from the following key:


I want to be able to populate a listview box with the subkeys from the above key.  Any ideas?  
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Title : HOWTO: Use the Registry API to Save and Retrieve Setting
Source :;EN-US;145679

Description :
Although Visual Basic includes the SaveSetting and GetSetting functions to save and retrieve information from the registry, these functions only operate on a specific section of the registry, the Visual Basic and VBA Program Settings of the HKEY_CURRENT_USER root key.

This article outlines the use of 32-bit Windows API functions, which can be used to set and retrieve values from anywhere in the registry. The topics and function references in this article can be generalized to program the 16-bit registry.

The 32-bit API functions also include support for security, although an overview of security is outside the scope of this article.

NOTE: The SaveSetting and GetSetting functions are not part of the VBA function library. However, the sample code below still applies to 32-bit applications that implement VBA.
General Registry Information
The registry is used by applications and Windows to store configuration data. It is a replacement for the large numbers of INI files that proliferated on Windows 3.x machines and is also used heavily by OLE.

The registry is organized using a hierarchical series of keys and values resembling a tree. Each key, beginning with one of the six predefined root keys, can have sub-keys and values associated with it. The keys are organizational and naming units and appear in the Windows Registry Editors as file folders. Values are data entries and appear as text entries in the right pane of the Registry Editor window. Keys need not have any associated values, but may have many. Each value has an associated data type. The two most commonly used registry data types are REG_SZ, a null-terminated string; and REG_DWORD, a 32-bit number.

The basic process used to write or read from a location in the registry is the same. To reference any given key or value, you must have a handle to the key. Once this handle is obtained, values and sub-keys of the key that this handle refers to can be read, set, or listed (enumerated).

Given a location in the registry, to obtain a handle to that key, you must begin with one of the six predefined keys (HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, and HKEY_DYN_DATA) and traverse the registry tree until the desired key is reached. User programs most often read and write from HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE. If the keys being traversed exist already, you can use a series of calls to the RegOpenKey or RegOpenKeyEx functions. If the keys need to be created, the RegCreateKey and RegCreateKeyEx functions do the job.

With the handle to the desired key, the functions used to list, set, and retrieve information can be called. In all cases, the functions with the Ex suffix will work only on 32-bit platforms. Functions without the suffix may work on both 16-bit and 32-bit versions of Windows. Keep in mind that not all registry functions lacking the 'Ex' suffix are functions provided for 16-bit compatibility. The Ex suffix was only added when the capabilities of 16-bit functions were expanded. Functions that are totally new and specific to 32-bit platforms do not possess an Ex extension.

The RegSetValue and RegSetValueEx functions allow the settings of a value to be modified, while RegQueryValue and RegQueryValueEx retrieve the current setting of a value. The limitations of the non-Ex, 16-bit versions of these APIs are very evident here. When using the 16-bit RegSetValue function there is no way to name a value, and because of this, RegSetValue can't be used to associate more than one value with each key. In addition, all values written with RegSetValue have a data type of REG_SZ. These limitations are inherent with the 16-bit Registry. RegSetValueEx allows the creation of a multiple number of values with any available data type.

more in the article

hope this helps a bit
You can still use the WSH Regread and Regwrite methods in VB.  Here's are two links on how to use both methods, and a sample from a VB6 app that I wrote... It shows you how to read a value and do something if it is what you expect.  Cheers!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

A one line modification of:

Add a commandbutton and a listbox, copy/paste the code, hit command1

Option Explicit
      Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
          Alias "RegOpenKeyExA" _
          (ByVal hKey As Long, _
          ByVal lpSubKey As String, _
          ByVal ulOptions As Long, _
          ByVal samDesired As Long, phkResult As Long) As Long
      Private Declare Function RegEnumValue Lib "advapi32.dll" _
          Alias "RegEnumValueA" _
          (ByVal hKey As Long, _
          ByVal dwIndex As Long, _
          ByVal lpValueName As String, _
          lpcbValueName As Long, _
          ByVal lpReserved As Long, _
          lpType As Long, _
          lpData As Any, _
          lpcbData As Long) As Long
      Private Declare Function RegCloseKey Lib "advapi32.dll" _
          (ByVal hKey As Long) As Long
      Const HKEY_CLASSES_ROOT = &H80000000
      Const HKEY_CURRENT_USER = &H80000001
      Const HKEY_LOCAL_MACHINE = &H80000002
      Const HKEY_USERS = &H80000003
      Const ERROR_SUCCESS = 0&
      Const SYNCHRONIZE = &H100000
      Const STANDARD_RIGHTS_READ = &H20000
      Const STANDARD_RIGHTS_WRITE = &H20000
      Const STANDARD_RIGHTS_ALL = &H1F0000
      Const KEY_QUERY_VALUE = &H1
      Const KEY_SET_VALUE = &H2
      Const KEY_CREATE_SUB_KEY = &H4
      Const KEY_NOTIFY = &H10
      Const KEY_CREATE_LINK = &H20
                        KEY_QUERY_VALUE Or _
                        KEY_ENUMERATE_SUB_KEYS Or _
                        KEY_NOTIFY) And _
                        (Not SYNCHRONIZE))
      Const REG_DWORD = 4
      Const REG_BINARY = 3
      Const REG_SZ = 1
      Private Sub Command1_Click()
         Dim lngKeyHandle As Long
         Dim lngResult As Long
         Dim lngCurIdx As Long
         Dim strValue As String
         Dim lngValueLen As Long
         Dim lngData As Long
         Dim lngDataLen As Long
         Dim strResult As String
         lngResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
                 "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", _
                  0&, _
                  KEY_READ, _
         If lngResult <> ERROR_SUCCESS Then
             MsgBox "Cannot open key"
             Exit Sub
         End If
         lngCurIdx = 0
            lngValueLen = 2000
            strValue = String(lngValueLen, 0)
            lngDataLen = 2000
            lngResult = RegEnumValue(lngKeyHandle, _
                                     lngCurIdx, _
                                     ByVal strValue, _
                                     lngValueLen, _
                                     0&, _
                                     REG_DWORD, _
                                     ByVal lngData, _
            lngCurIdx = lngCurIdx + 1
         If lngResult = ERROR_SUCCESS Then
            strResult = lngCurIdx & ": " & Left(strValue, lngValueLen)
            List1.AddItem strResult
         End If
         Loop While lngResult = ERROR_SUCCESS
         Call RegCloseKey(lngKeyHandle)
      End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
<< subkeys from the above key.  Any ideas?   >>

If you want subkeys than you use RegEnumKeyEx()
Option Explicit
Private Const BUF_SIZE As Long = 512
Private Const ERROR_SUCCESS As Long = 0
Private Const ERROR_NO_MORE_ITEMS As Long = 259
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Declare Function RegOpenKeyExW Lib "Advapi32" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegEnumKeyExW Lib "Advapi32" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As Long, lpcName As Long, ByVal lpReserved As Long, ByVal lpClass As Long, ByVal lpcClass As Long, lpfLastWrite As FILETIME) As Long
Private Declare Function RegCloseKey Lib "Advapi32" (ByVal hKey As Long) As Long
Sub GetRegSubKeys(ByVal szSubkey As String)
  ' szSubkey : Software\Microsoft\Windows\CurrentVersion\Run
  Dim bSubKey(BUF_SIZE) As Byte
  Dim udtFt As FILETIME
  Dim Success As Long
  Dim dwIndex As Long
  Dim dwLength As Long
  Dim lpKey As Long
    dwIndex = 0
      ' Reset buffer size for next call.
      dwLength = BUF_SIZE
      ' Get the subkey information.
      Success = RegEnumKeyExW(lpKey, dwIndex, VarPtr(bSubKey(0)), dwLength, 0, 0, 0, udtFt)
      If Success = ERROR_SUCCESS Then
      ' Increment the dwIndex parameter and call
      ' RegEnumKeyEx until there are no more subkeys.
        dwIndex = (dwIndex + 1)
      ' TODO:// add to listbox
        Debug.Print Left$(bSubKey, dwLength)
        ' Just bail
        Exit Do
      End If
    Loop Until Success = ERROR_NO_MORE_ITEMS
  End If
  ' --- * free handle
  RegCloseKey lpKey
  ' --- * free memory
  Erase bSubKey
End Sub

Open in new window

471094Author Commented:
Answered my question perfectly!  Thank You!
471094Author Commented:
Ok, this may sound really bad but I got the subkeys to list correctly in the listbox but now I need to be able to delete a selected key???  
You could just use RegDelete of the WshShell for that [Windows Script Host Object Model].

dim ws as new WshShell


471094Author Commented:
Is that statement in a particular type library?  I got an error saying "User defined type not allowed"
471094Author Commented:
Nevermind, I spoke too soon, I answered my own question.  Sorry about that.  I refrenced WSHOM
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.