All pastes #258546 Raw Edit

Anonymous

public text v1 · immutable
#258546 ·published 2006-11-26 17:00 UTC
rendered paste body

' Windows Registry Save and Loading of Values
' Credit: VB-World


Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&

' Registry API Prototypes
' =======================
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
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
Public Const REG_SZ = 1                         ' Unicode nul terminated string
Public Const REG_DWORD = 4                      ' 32-bit number

Public Sub CreateKey(Hkey As Long, strPath As String)
    Dim Keyhand&
    
    R = RegCreateKey(Hkey, strPath, Keyhand&)
    R = RegCloseKey(Keyhand&)
End Sub

Public Sub Savekey(Hkey As Long, strPath As String)
    Dim Keyhand&
    R = RegCreateKey(Hkey, strPath, Keyhand&)
    R = RegCloseKey(Keyhand&)
End Sub

Public Function Getstring(Hkey As Long, strPath As String, strValue As String)
    Dim Keyhand As Long
    Dim Datatype As Long, lResult As Long, strBuf As String
    Dim lDataBufSize As Long, intZeroPos As Integer

    R = RegOpenKey(Hkey, strPath, Keyhand)

    lResult = RegQueryValueEx(Keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)

    If lValueType = REG_SZ Then
        strBuf = String(lDataBufSize, " ")
        lResult = RegQueryValueEx(Keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
    
        If lResult = ERROR_SUCCESS Then
            intZeroPos = InStr(strBuf, Chr$(0))
        
            If intZeroPos > 0 Then
                Getstring = Left$(strBuf, intZeroPos - 1)
            Else
                Getstring = strBuf
            End If
        End If
    End If
End Function

Public Sub Savestring(Hkey As Long, strPath As String, strValue As String, strdata As String)
    Dim Keyhand As Long, R As Long
    
    R = RegCreateKey(Hkey, strPath, Keyhand)
    R = RegSetValueEx(Keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
    R = RegCloseKey(Keyhand)
End Sub

Function GetDWord(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String) As Long

    Dim lResult As Long, lValueType As Long, lBuf As Long
    Dim lDataBufSize As Long, R As Long, Keyhand As Long

    R = RegOpenKey(Hkey, strPath, Keyhand)

    ' Get length/data type
    lDataBufSize = 4
    
    lResult = RegQueryValueEx(Keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

    If lResult = ERROR_SUCCESS Then
        If lValueType = REG_DWORD Then
            GetDWord = lBuf
        End If
    'Else
    ' Call errlog("GetDWORD-" & strPath, False)
    End If

    R = RegCloseKey(Keyhand)
    
End Function

Function SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
    Dim lResult As Long, Keyhand As Long, R As Long
    
    R = RegCreateKey(Hkey, strPath, Keyhand)
    
    lResult = RegSetValueEx(Keyhand, strValueName, 0&, REG_DWORD, lData, 4)
    
    'If lResult <> error_success Then Call errlog("SetDWORD", False)
    
    R = RegCloseKey(Keyhand)
End Function

Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
    Dim R As Long

    R = RegDeleteKey(Hkey, strKey)
End Function

Public Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
    Dim Keyhand As Long

    R = RegOpenKey(Hkey, strPath, Keyhand)
    R = RegDeleteValue(Keyhand, strValue)
    R = RegCloseKey(Keyhand)
End Function