Solved

Retreiving Subkeys in the registry for VB6

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

Expert Comment

by:game-master
ID: 24837346
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
0
 
LVL 13

Expert Comment

by:game-master
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!

http://www.devguru.com/Technologies/wsh/quickref/wshshell_RegRead.html
http://www.devguru.com/Technologies/wsh/quickref/wshshell_RegWrite.html
0
 
LVL 13

Expert Comment

by:game-master
ID: 24837362
0
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

 
LVL 10

Accepted Solution

by:
peetm earned 250 total points
ID: 24837961
A one line modification of:

http://support.microsoft.com/kb/178755

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_EXECUTE = &H20000
      Const STANDARD_RIGHTS_REQUIRED = &HF0000
      Const STANDARD_RIGHTS_ALL = &H1F0000
      Const KEY_QUERY_VALUE = &H1
      Const KEY_SET_VALUE = &H2
      Const KEY_CREATE_SUB_KEY = &H4
      Const KEY_ENUMERATE_SUB_KEYS = &H8
      Const KEY_NOTIFY = &H10
      Const KEY_CREATE_LINK = &H20
      Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
                        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, _
                  lngKeyHandle)
 
         If lngResult <> ERROR_SUCCESS Then
             MsgBox "Cannot open key"
             Exit Sub
         End If
 
         lngCurIdx = 0
         Do
            lngValueLen = 2000
            strValue = String(lngValueLen, 0)
            lngDataLen = 2000
 
            lngResult = RegEnumValue(lngKeyHandle, _
                                     lngCurIdx, _
                                     ByVal strValue, _
                                     lngValueLen, _
                                     0&, _
                                     REG_DWORD, _
                                     ByVal lngData, _
                                     lngDataLen)
            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

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
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
  
  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

0
 

Author Closing Comment

by:471094
ID: 31602695
Answered my question perfectly!  Thank You!
0
 

Author Comment

by:471094
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???  
0
 
LVL 10

Expert Comment

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

dim ws as new WshShell

ws.RegDelete(...)

0
 

Author Comment

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

Author Comment

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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

739 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