Attribute VB_Name = "Registry"
Public 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
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long

Public Enum RegistryPaths
    HKEY_CLASSES_ROOT = &H80000000
    HKEY_CURRENT_USER = &H80000001
    HKEY_LOCAL_MACHINE = &H80000002
End Enum

Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
    
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Function ReadValue(RegistryDir As RegistryPaths, SubDirectory As String, ValueName As String)
Dim ReturnValue As Long
Dim ReturnKeyName As Long
Dim ValueBuffer As String
Dim ValueSize As Long
Dim DataType As Long

ReturnValue = RegOpenKeyEx(RegistryDir, SubDirectory, 0, KEY_READ, ReturnKeyName)

If ReturnValue = 0 Then
    ValueBuffer = Space(2048)
    ValueSize = 2048
    ReturnValue = RegQueryValueEx(ReturnKeyName, ValueName, 0, DataType, ByVal ValueBuffer, ValueSize)
    ValueBuffer = Left(ValueBuffer, ValueSize - 1)
    ReturnValue = RegCloseKey(ReturnKeyName)
    ReadValue = ValueBuffer
End If
End Function
Public Function DeleteKey(RegistryDir As RegistryPaths, SubDirectory As String)
Dim ReturnValue As Long

ReturnValue = RegDeleteKey(RegistryDir, SubDirectory)
End Function

Function EnumKey(ByVal hKey As RegistryPaths, ByVal SubKey As String)
Dim KeyName As String
Dim KeyLen As Long
Dim ClassName As String
Dim ClassLen As Long
Dim LastWrite As FILETIME
Dim Temp As Long
Dim Index As Long
Dim retval As Long
Dim FoundPath As String
    
retval = RegOpenKeyEx(hKey, SubKey, 0, KEY_ENUMERATE_SUB_KEYS, Temp)

Index = 0
While retval = 0
    KeyName = Space(255)
    ClassName = Space(255)
    KeyLen = 255
    ClassLen = 255

    retval = RegEnumKeyEx(Temp, Index, KeyName, KeyLen, ByVal 0, ClassName, ClassLen, LastWrite)
      
    If retval = 0 Then
        KeyName = Left(KeyName, KeyLen)
        ClassName = Left(ClassName, ClassLen)

        FoundPath = KeyName
        
        'put all keys found in the AllKeys() Array
        ReDim Preserve AllKeys(UBound(AllKeys) + 1) As String
        AllKeys(UBound(AllKeys)) = FoundPath
    End If
    Index = Index + 1
Wend
    
retval = RegCloseKey(Temp)
End Function
