Attribute VB_Name = "modRegistry"
Option Explicit

'WIN32API Costant
Public Const REG_SZ = 1                         ' Unicode nul terminated string
Public Const REG_BINARY = 3                     ' Free form binary
Public Const REG_DWORD = 4                      ' 32-bit number

Public Const MAX_PATH = 260
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_NO_MORE_ITEMS = 259&

'WIN32 API declaration
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Public 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

Public hCurKey As Long

Public Function DeleteKey(ByVal hKey As Long, ByVal lpSubKey As String) As Long
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Then GoTo ErrHandle
    'Open the given hkey + SubKey
    If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Delete the given String from given hkey + SubKey
        If RegDeleteKey(hCurKey, lpSubKey) = ERROR_SUCCESS Then
            DeleteKey = 1
        Else
            DeleteKey = 0
        End If
    Else
        DeleteKey = 0
    End If
    RegCloseKey hCurKey
Exit Function
ErrHandle:
    DeleteKey = 0
End Function

Public Function DeleteString(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpString As String) As Long
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
    'Open the given hkey + SubKey
    If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Delete the given String from given hkey + SubKey
        If RegDeleteValue(hCurKey, lpString) = ERROR_SUCCESS Then
            DeleteString = 1
        Else
            DeleteString = 0
        End If
    Else
        DeleteString = 0
    End If
    RegCloseKey hCurKey
Exit Function
ErrHandle:
    DeleteString = 0
End Function

Public Function GetSettings(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long, Optional ByVal lpDefault As Variant) As Variant
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
    
    'Open the given hkey + subkey
    If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Retrieve the given string value
        Select Case dwType
        Case REG_SZ
            Dim strBuff As String
            strBuff = String(255, Chr(0))
            
            If RegQueryValueEx(hCurKey, lpString, 0, dwType, ByVal strBuff, Len(strBuff)) = ERROR_SUCCESS Then
                GetSettings = Left(strBuff, InStr(1, strBuff, Chr(0), vbTextCompare) - 1)
            Else
                GetSettings = lpDefault
            End If
        Case REG_BINARY
            Dim strData As Integer
            
            If RegQueryValueEx(hCurKey, lpString, 0, dwType, strData, 4) = ERROR_SUCCESS Then
                GetSettings = strData
            Else
                GetSettings = lpDefault
            End If
        Case REG_DWORD
            Dim strDWORD As Long
            
            If RegQueryValueEx(hCurKey, lpString, 0, dwType, strDWORD, 4) = ERROR_SUCCESS Then
                GetSettings = strDWORD
            Else
                GetSettings = lpDefault
            End If
        End Select
    Else
        GetSettings = lpDefault
    End If
    RegCloseKey hCurKey
    
    Exit Function
ErrHandle:
    GetSettings = lpDefault
End Function
Public Function CreateKey(ByVal hKey As Long, ByVal lpSubKey As String) As Long
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Then GoTo ErrHandle
    If RegCreateKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        CreateKey = 1
    Else
        CreateKey = 0
    End If
    RegCloseKey hCurKey
    Exit Function
    
ErrHandle:
    CreateKey = 0
End Function

Public Function CreateString(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long, ByVal lpData As String) As Long
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
    
    'Open the given hkey + subkey
    If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Create the given string + value
        If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
            CreateString = 1
        Else
            CreateString = 0
        End If
    Else
        'Create the given hkey + subkey
        If RegCreateKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
            'Create the given string + value
            If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
                CreateString = 1
            Else
                CreateString = 0
            End If
        Else
            CreateString = 0
        End If
    End If
    RegCloseKey hCurKey
    
Exit Function
ErrHandle:
    CreateString = 0
End Function

Public Function SaveSettings(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long, ByVal lpData As String) As Long
On Error GoTo ErrHandle
    'Data validation
    If hKey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
    
    'Open the given hkey + subkey
    If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Create the given string + value
        Select Case dwType
        Case REG_SZ
            If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
                SaveSettings = 1
            Else
                SaveSettings = 0
            End If
        Case REG_BINARY
            If RegSetValueEx(hCurKey, lpString, 0, dwType, CByte(lpData), 4) = ERROR_SUCCESS Then
                SaveSettings = 1
            Else
                SaveSettings = 0
            End If
        Case REG_DWORD
            If RegSetValueEx(hCurKey, lpString, 0, dwType, CByte(lpData), 16) = ERROR_SUCCESS Then
                SaveSettings = 1
            Else
                SaveSettings = 0
            End If
        End Select
    Else
        If CreateKey(hKey, lpSubKey) = 1 Then
            If RegOpenKey(hKey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
                'Create the given string + value
                Select Case dwType
                Case REG_SZ
                    If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
                        SaveSettings = 1
                    Else
                        SaveSettings = 0
                    End If
                Case REG_BINARY
                    If RegSetValueEx(hCurKey, lpString, 0, dwType, CByte(lpData), 4) = ERROR_SUCCESS Then
                        SaveSettings = 1
                    Else
                        SaveSettings = 0
                    End If
                Case REG_DWORD
                
                End Select
            End If
        Else
            SaveSettings = 0
        End If
    End If
    RegCloseKey hCurKey
    
Exit Function
ErrHandle:
    SaveSettings = 0
End Function

'Public Function EnumKey(ByVal hKey As Long, ByVal lpSubKey As String) As Variant
'    Dim Cnt As Long
'    Dim Buff As String
'    Dim Data() As String
'
'    RegOpenKey hKey, lpSubKey, hCurKey
'    Cnt = 0
'    Erase Data
'    Do
'        Buff = String(MAX_PATH + 1, 0)
'        'Enum the given registry key
'        If RegEnumKey(hCurKey, Cnt, Buff, MAX_PATH + 1) = ERROR_NO_MORE_ITEMS Then Exit Do
'        'Save the string into array
'        ReDim Preserve Data(Cnt)
'        Data(Cnt) = Left(Buff, InStr(1, Buff, Chr(0), vbTextCompare) - 1)
'        'Increase the counter
'        Cnt = Cnt + 1
'        DoEvents
'    Loop
'    RegCloseKey hKey
'    EnumKey = Data
'End Function
'
'Public Function EnumValue(ByVal hKey As Long, ByVal lpSubKey As String) As Variant
'    Dim Cnt As Long
'    Dim Buff As String
'    Dim Data() As String
'
'    RegOpenKey hKey, lpSubKey, hCurKey
'    Cnt = 0
'    Erase Data
'    Do
'        Buff = String(255, 0)
'        'Enum the given registry key
'        If RegEnumValue(hCurKey, Cnt, Buff, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) = ERROR_NO_MORE_ITEMS Then Exit Do
'        'Save the string into array
'        ReDim Preserve Data(Cnt)
'        Data(Cnt) = Left(Buff, InStr(1, Buff, Chr(0), vbTextCompare) - 1)
'        'Increase the counter
'        Cnt = Cnt + 1
'        DoEvents
'    Loop
'    RegCloseKey hKey
'    EnumValue = Data
'End Function


