Results 1 to 5 of 5

Thread: how to output registery folder names and key inside it in to listbox?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved how to output registery folder names and key inside it in to listbox?

    Hi all .

    I have a problem retrieve the usernames and Id stored in HKEY_CURRENT_USER\Software\xyz\123\

    usernames are not not stored as string, the usernames are stored as the folders name inside of the 123 directory. For example, the program has 3 usernames stored in the registry; cat, panda, lion. So these are the folders :

    HKEY_CURRENT_USER\Software\xyz\123\cat\
    HKEY_CURRENT_USER\Software\xyz\123\panda\
    HKEY_CURRENT_USER\Software\xyz\123\lion\

    inside each username folder there is a key called Id and I want to print the username foldernames and the value of ID key inside each usename folder into a listbox. I appreacite if any expert show me how to achive this task.Thanks
    Last edited by tony007; Jun 16th, 2006 at 02:30 AM.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: how to output registery folder names and key inside it in to listbox?

    Primarily you need to utilize the RegEnumKeyEx and RegQueryValueEx Win32 API's, i.e.

    In a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. ' Win32 API Declarations
    4. Private Const SYNCHRONIZE = &H100000
    5. Private Const READ_CONTROL = &H20000
    6. Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    7.  
    8. Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    9. Private Const KEY_NOTIFY = &H10
    10. Private Const KEY_QUERY_VALUE = &H1
    11.  
    12. Private Const KEY_READ = (( _
    13.   STANDARD_RIGHTS_READ Or _
    14.   KEY_QUERY_VALUE Or _
    15.   KEY_ENUMERATE_SUB_KEYS Or _
    16.   KEY_NOTIFY) And (Not SYNCHRONIZE))
    17.  
    18. Private Const HKEY_LOCAL_MACHINE = &H80000002
    19. Private Const HKEY_USERS = &H80000003
    20. Private Const HKEY_CURRENT_CONFIG = &H80000005
    21. Private Const HKEY_CURRENT_USER = &H80000001
    22. Private Const HKEY_CLASSES_ROOT = &H80000000
    23.  
    24. Private Const ERROR_SUCCESS = 0&
    25.  
    26. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
    27.   Alias "RegOpenKeyExA" (ByVal hKey As Long, _
    28.   ByVal lpSubKey As String, ByVal ulOptions As Long, _
    29.   ByVal samDesired As Long, phkResult As Long) As Long
    30.  
    31. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" _
    32.   Alias "RegEnumKeyExA" (ByVal hKey As Long, _
    33.   ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, _
    34.   ByVal lpReserved As Long, ByVal lpClass As String, _
    35.   lpcbClass As Long, lpftLastWriteTime As Any) As Long
    36.  
    37. Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
    38.   Alias "RegQueryValueExA" (ByVal hKey As Long, _
    39.   ByVal lpValueName As String, ByVal lpReserved As Long, _
    40.   lpType As Long, lpData As Any, lpcbData As Long) As Long
    41.  
    42. Private Declare Function RegCloseKey Lib "advapi32.dll" _
    43.   (ByVal hKey As Long) As Long
    44.  
    45. ' Define an enumration of the
    46. ' different registry hive types
    47. Public Enum RegistryHives
    48.   LocalMachine = HKEY_LOCAL_MACHINE
    49.   Users = HKEY_USERS
    50.   CurrentConfig = HKEY_CURRENT_CONFIG
    51.   CurrentUser = HKEY_CURRENT_USER
    52.   ClassesRoot = HKEY_CLASSES_ROOT
    53. End Enum
    54.  
    55.  
    56. Public Function GetRegKeys( _
    57.   ByVal hive As RegistryHives, _
    58.   ByVal subKeyPath As String, _
    59.   ByRef keyList() As String) As Long
    60.  
    61.   Dim lngKey As Long
    62.   Dim lngResult As Long
    63.   Dim lngSecurity As Long
    64.   Dim lngIndex As Long
    65.  
    66.   Dim lngKeyNameLength As Long
    67.   Dim strKeyName As String
    68.  
    69.   ' Request read access to the registry key
    70.   lngSecurity = KEY_READ
    71.  
    72.   ' Open the key in the specified hive
    73.   lngResult = RegOpenKeyEx( _
    74.     hive, subKeyPath, 0, lngSecurity, lngKey)
    75.  
    76.   ' If we couldn't open the key, abort
    77.   If lngResult <> ERROR_SUCCESS Then
    78.     GetRegKeys = -1
    79.     Exit Function
    80.   End If
    81.    
    82.   ' Start at the first sub-key in the Key (Zero Index)
    83.   lngIndex = 0
    84.   Do
    85.     ' Reset the key name variables creating a
    86.     ' buffer to receive the next keys' name
    87.     lngKeyNameLength = 255
    88.     strKeyName = String(lngKeyNameLength, Chr(0))
    89.     ' Ask for the next sub-key in the key
    90.     lngResult = RegEnumKeyEx(lngKey, lngIndex, _
    91.       strKeyName, lngKeyNameLength, 0, vbNullString, ByVal 0&, ByVal 0&)
    92.      
    93.     ' If successful, store the name in the
    94.     ' array to be returned
    95.     If lngResult = ERROR_SUCCESS Then
    96.       ' Strip out null characters from the buffered string
    97.       strKeyName = Trim(Replace(strKeyName, Chr(0), ""))
    98.       ' Add the key name to the array
    99.       ReDim Preserve keyList(lngIndex)
    100.       keyList(lngIndex) = strKeyName
    101.     End If
    102.     ' Increment the sub-key index
    103.     lngIndex = lngIndex + 1
    104.    
    105.     ' Keep going until there aren't any more
    106.     ' sub-keys in this key
    107.   Loop While lngResult = ERROR_SUCCESS
    108.  
    109.   ' Close the registry key
    110.   Call RegCloseKey(lngKey)
    111.  
    112.   ' Return the number of sub-keys found
    113.   GetRegKeys = lngIndex - 1
    114. End Function
    115.  
    116. Public Function GetRegKeyValue( _
    117.   ByVal hive As RegistryHives, ByVal subKeyPath As String, _
    118.   ByVal valueName As String) As String
    119.  
    120.   Dim lngKey As Long
    121.   Dim lngResult As Long
    122.   Dim lngSecurity As Long
    123.  
    124.   Dim lngDataLength As Long
    125.   Dim lngDataType As Long
    126.  
    127.   Dim bytData() As Byte
    128.  
    129.   ' Request read access to the registry key
    130.   lngSecurity = KEY_READ
    131.  
    132.   ' Open the key in the specified hive
    133.   lngResult = RegOpenKeyEx( _
    134.     hive, subKeyPath, 0, lngSecurity, lngKey)
    135.  
    136.   ' If we couldn't open the key, abort
    137.   If lngResult <> ERROR_SUCCESS Then
    138.     Exit Function
    139.   End If
    140.    
    141.   ' Query the value name supplied to determine the
    142.   ' type and size of the data it contains
    143.   lngResult = RegQueryValueEx(lngKey, _
    144.     valueName, 0, lngDataType, ByVal 0&, lngDataLength)
    145.        
    146.   ' If successful, the lngDataLength variable will
    147.   ' contain info about the size of the buffer needed
    148.   ' to correctly extract the data
    149.   If lngResult = ERROR_SUCCESS Then
    150.     ' Resize the data byte-array to the necessary size
    151.     ReDim bytData(lngDataLength + 1) As Byte
    152.     ' Call the same function,
    153.     ' this time extracting the data contents
    154.     lngResult = RegQueryValueEx(lngKey, _
    155.       valueName, 0, lngDataType, bytData(0), lngDataLength)
    156.    
    157.     If lngResult = ERROR_SUCCESS Then
    158.       ' If successful, strip out terminating characters
    159.       GetRegKeyValue = Replace(StrConv(bytData, vbUnicode), Chr(0), "")
    160.     End If
    161.   End If
    162.  
    163.   ' Close the registry key
    164.   Call RegCloseKey(lngKey)
    165. End Function
    In a Form with a Command Button and ListBox:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Dim strUsers() As String
    5.   Dim strUser As String
    6.   Dim strKey As String
    7.   Dim strId As String
    8.   Dim lngKeyCount As Long
    9.   Dim lngKeyIndex As Long
    10.  
    11.   ' The Key to look at
    12.   strKey = "Software\XYZ\123"
    13.  
    14.   ' Get an array of Sub-Keys
    15.   lngKeyCount = GetRegKeys( _
    16.     CurrentUser, strKey, strUsers)
    17.  
    18.   For lngKeyIndex = 0 To lngKeyCount - 1
    19.     ' Get the username
    20.     strUser = strUsers(lngKeyIndex)
    21.     ' Now query the registry for this user's "Id" value
    22.     strId = GetRegKeyValue( _
    23.       CurrentUser, strKey & "\" & strUser, "Id")
    24.     ' Add them to the ListBox
    25.     List1.AddItem strUser & " [Id = " & strId & "]"
    26.   Next
    27. End Sub
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: how to output registery folder names and key inside it in to listbox?

    Aaron Young Many Many thanks for ur perfect answer. I just want to know if i want output anothe key from each folder beside ID how i can add that to search string. Thanks

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: how to output registery folder names and key inside it in to listbox?

    Just add additional calls to the GetRegKeyValue() function, i.e.
    VB Code:
    1. ...
    2.     ' Get the username
    3.     strUser = strUsers(lngKeyIndex)
    4.     ' Now query the registry for this user's "Id" value
    5.     strId = GetRegKeyValue( _
    6.       CurrentUser, strKey & "\" & strUser, "Id")
    7.    
    8.     strOtherValue1 = GetRegKeyValue( _
    9.       CurrentUser, strKey & "\" & strUser, "OtherValueName")
    10.  
    11.     strOtherValue2 = GetRegKeyValue( _
    12.       CurrentUser, strKey & "\" & strUser, "OtherValue2Name")
    13.     ...
    Regards,

    - Aaron.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: RESOLVED:how to output registery folder names and key inside it in to listbox?

    Aaron Young many thanks for u previouse help. Now i want to delete multiple regisery key valuse that are in listbox by selecting them and press a button i want to remove those selected ones. i be happy if u show me how i can delete them from registery. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width