Retreiving Subkeys in the registry for VB6

Posted on 2009-07-12
Last Modified: 2012-05-07
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?  
Question by:471094
  • 4
  • 3
  • 2
  • +1
LVL 13

Expert Comment

ID: 24837346
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
LVL 13

Expert Comment

ID: 24837349
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!
LVL 13

Expert Comment

ID: 24837362
LVL 10

Accepted Solution

peetm earned 250 total points
ID: 24837961
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

LVL 29

Expert Comment

ID: 24839701
<< 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.


Author Closing Comment

ID: 31602695
Answered my question perfectly!  Thank You!

Author Comment

ID: 24843805
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???  
LVL 10

Expert Comment

ID: 24844256
You could just use RegDelete of the WshShell for that [Windows Script Host Object Model].

dim ws as new WshShell



Author Comment

ID: 24844287
Is that statement in a particular type library?  I got an error saying "User defined type not allowed"

Author Comment

ID: 24844301
Nevermind, I spoke too soon, I answered my own question.  Sorry about that.  I refrenced WSHOM

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows pro to home 2 70
String manipulation in Visual Basic 7 44
using Access 8 52
Copy a row 12 53
I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now