Link to home
Start Free TrialLog in
Avatar of 471094
471094

asked on

Retreiving Subkeys in the registry for VB6

Hello,
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:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

I want to be able to populate a listview box with the subkeys from the above key.  Any ideas?  
Avatar of game-master
game-master
Flag of Philippines image

Title : HOWTO: Use the Registry API to Save and Retrieve Setting
Source : http://support.microsoft.com/default.aspx?scid=kb;EN-US;145679

Description :
-----------
SUMMARY
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.
MORE INFORMATION
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!

http://www.devguru.com/Technologies/wsh/quickref/wshshell_RegRead.html
http://www.devguru.com/Technologies/wsh/quickref/wshshell_RegWrite.html
ASKER CERTIFIED SOLUTION
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

<< 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
  
  If RegOpenKeyExW(HKEY_CURRENT_USER, StrPtr(szSubkey), 0, KEY_ENUMERATE_SUB_KEYS, lpKey) = ERROR_SUCCESS Then
    
    dwIndex = 0
    
    Do
      ' 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)
    
      Else
        ' 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

Avatar of 471094

ASKER

Answered my question perfectly!  Thank You!
Avatar of 471094

ASKER

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

ws.RegDelete(...)

Avatar of 471094

ASKER

Is that statement in a particular type library?  I got an error saying "User defined type not allowed"
Avatar of 471094

ASKER

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