Results 1 to 7 of 7

Thread: Registry Question.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    18
    I know how to read and write to the registry, but is there a way to get an entire registry keys contents with all the strings and, strings names??

  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Yes.



    Search this site, search the Web, search MSDN, search APILoad.

    To get you started:
    • RegOpenKeyEx
    • RegCloseKey
    • RegEnumKeyEx
    • RegEnumValue
    • RegQueryInfoKey
    • RegQueryValueEx
    • RegSetValue

    You should also look up the Registry data-types (constants pre-fixed with REG_) and the main Registry keys (constants pre-fixed with HKEY_).

    Sorry about this crappy response, I'm tired... maybe I'll post a better response tomorrow.

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Sorry about that last post, I had just finished writing my thread posting on IEEE floating point formats (which you'll notice is pretty long if you take look at it) and I was really tired. Anyway here's the code to list all of the Value Names and their corresponding Data:

    Declarations
    Code:
    '<<< CONSTANTS >>>
    'Variable maximum sizes.
    Private Const MAX_PATH As Integer = 260
    Private Const MAXBYTE As Integer = &HFF
    Private Const MAXCHAR As Integer = &H7F
    Private Const MAXSHORT As Integer = &H7FFF
    
    'Error codes.
    Private Const ERROR_SUCCESS As Long = 0
    Private Const ERROR_MORE_DATA As Long = 234
    
    'Top-level registry Keys.
    Private Const HKEY_CLASSES_ROOT As Long = &H80000000
    Private Const HKEY_CURRENT_USER As Long = &H80000001
    Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
    Private Const HKEY_USERS As Long = &H80000003
    Private Const HKEY_PERFORMANCE_DATA As Long = &H80000004
    Private Const HKEY_CURRENT_CONFIG As Long = &H80000005
    Private Const HKEY_DYN_DATA As Long = &H80000006
    
    'Registry data types.
    Private Const REG_NONE As Integer = 0
    Private Const REG_SZ As Integer = 1
    Private Const REG_EXPAND_SZ As Integer = 2
    Private Const REG_BINARY As Integer = 3
    Private Const REG_DWORD As Integer = 4
    Private Const REG_DWORD_LITTLE_ENDIAN As Integer = 4
    Private Const REG_DWORD_BIG_ENDIAN As Integer = 5
    Private Const REG_LINK As Integer = 6
    Private Const REG_MULTI_SZ As Integer = 7
    Private Const REG_RESOURCE_LIST As Integer = 8
    Private Const REG_FULL_RESOURCE_DESCRIPTOR As Integer = 9
    Private Const REG_RESOURCE_REQUIREMENTS_LIST As Integer = 10
    
    'Object security constants.
    Private Const READ_CONTROL As Long = &H20000
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const SPECIFIC_RIGHTS_ALL As Long = &HFFFF
    Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
    Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000
    Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
    Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
    
    'Registry Key operation request flags.
    Private Const KEY_EVENT As Long = &H1
    Private Const KEY_QUERY_VALUE As Long = &H1
    Private Const KEY_SET_VALUE As Long = &H2
    Private Const KEY_CREATE_SUB_KEY As Long = &H4
    Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
    Private Const KEY_NOTIFY As Long = &H10
    Private Const KEY_CREATE_LINK As Long = &H20
    Private Const KEY_READ As Long = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE _
                                     Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Private Const KEY_WRITE As Long = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE _
                                      Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Private Const KEY_EXECUTE As Long = ((KEY_READ) And (Not SYNCHRONIZE))
    Private Const KEY_ALL_ACCESS As Long = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE _
                                           Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY _
                                           Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY _
                                           Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    
    '<<< TYPES >>>
    Private Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type
    
    '<<< DECLARES >>>
    Private Declare Function RegCloseKey _
                    Lib "advapi32.dll" _
                    (ByVal hKey As Long) As Long
    
    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 Byte, lpcbData As Long) As Long
    
    Private Declare Function RegQueryInfoKey _
                    Lib "advapi32.dll" _
                    Alias "RegQueryInfoKeyA" _
                    (ByVal hKey As Long, ByVal lpClass As String, _
                    lpcbClass As Long, ByVal lpReserved As Long, _
                    lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, _
                    lpcbMaxClassLen As Long, lpcValues As Long, _
                    lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, _
                    lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
    Procedure
    Places all of the Value Names and Data into a ListBox control named List1. The way I coded it for this example, the procedure only retrieves regular strings (REG_SZ) from the registry. In order to write a fully functional procedure you would need to take into account all of the registry data-types.
    Sorry it's all in one procedure, I typed it up quickly
    Code:
    Private Sub ListRegValues(hKey As Long, sSubKey As String)
       Dim lRet As Long
       Dim hRegKey As Long
       Dim sClass As String
       Dim lClass As Long
       Dim lNumSubKeys As Long
       Dim lMaxSubKeyLen As Long
       Dim lMaxClassLen As Long
       Dim lNumValues As Long
       Dim lMaxValueNameLen As Long
       Dim lMaxValueDataLen As Long
       Dim lSecurityDescriptorLen As Long
       Dim udtModificationTime As FILETIME
       Dim idx As Integer
       Dim sValueName As String
       Dim lNameLen As Long
       Dim bytValueData() As Byte
       Dim lDataLen As Long
       Dim lDataType As Long
       
       lRet = RegOpenKeyEx(hKey, sSubKey, 0, KEY_READ, hRegKey)
              
       If lRet = ERROR_SUCCESS Then
          sClass = String(MAX_PATH, 0)
          lClass = Len(sClass) - 1
          lRet = RegQueryInfoKey(hRegKey, sClass, lClass, 0, lNumSubKeys, lMaxSubKeyLen, _
                 lMaxClassLen, lNumValues, lMaxValueNameLen, lMaxValueDataLen, _
                 lSecurityDescriptorLen, udtModificationTime)
          
          If lRet = ERROR_SUCCESS Then
             Call List1.Clear
             ReDim bytValueData(lMaxValueDataLen + 1)
             
             For idx = 0 To lNumValues - 1 Step 1
                lNameLen = lMaxValueNameLen + 1
                sValueName = String(lNameLen, 0)
                lDataLen = lMaxValueDataLen + 1
                lRet = RegEnumValue(hRegKey, idx, sValueName, lNameLen, 0, _
                       lDataType, bytValueData(0), lDataLen)
                
                If lRet = ERROR_SUCCESS _
                And lDataType = REG_SZ Then
                   
                   If lDataLen > 0 Then
                      lDataLen = lDataLen - 1
                   End If
                   
                   Call List1.AddItem(Left(sValueName, lNameLen) & " = " & _
                        Left(StrConv(bytValueData, vbUnicode), lDataLen))
                End If
                
             Next idx
             
          End If
          
          Call RegCloseKey(hRegKey)
       End If
    End Sub
    To call the procedure
    Code:
    Private Sub Form_Load()
       Call ListRegValues(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
    End Sub
    Hope that this helps.
    Note: To enumerate all of the SubKeys is quite similar to this method, just use the RegEnumKeyEx function.

    Later.

    [Edited by SonGouki on 04-08-2000 at 05:22 PM]

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    18
    Hi SonGouki

    thanx for helping, but the code you gave me gives me an error.
    ByReg argument type mismatch.
    Is there a way to fix that?

  5. #5
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    ?

    Did you try copying the code exactly as is into a blank project first? Do that and see if it works there, then modify it and port it over to the project where you want to use it. I just tried copying the code into a new project and it worked fine. Then again what version of Windows are you using? I'm using '95. If your using 2000 there are extra things to take into to consideration, like security.

    Post a response and let me know if you got it to work or not.

  6. #6
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    Hi When I tried the code, I just dumped it all in a module, except for the list reg sub and the form load sub, because I am used to registry codes having to be in a module. And I got the ByReg error. So I just took it all out of the module and put it in the forms code and it worked fine
    Great code by the way

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Happy to help.

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