Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Registry.inc - easy general purpose registry access

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Registry.inc - easy general purpose registry access

    Code:
    'REGISTRY.INC
    'General Purpose Registry Access
    'Uses: win32api.inc
    
    'Functions:
    ' DelRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As Long
    ' GetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As String
    ' SetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String, ByVal Setting As String) As Long
    ' DelRegKey(lpKey As Long, Key As String) Export As Long
     
    Function DelRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As Long
    On Error Resume Next
    Local RetCode As Long
    Local hKey As Long
    Dim acMainkey As AsciiZ * 300
         acMainkey = cMainkey
         RetCode = RegOpenKeyEx(lpKey, acMainkey, 0&, %KEY_ALL_ACCESS, hKey)
         If RetCode = %ERROR_SUCCESS Then
           If Key$ = "*" Then Key$ = Chr$(0,0)
           RetCode = RegDeleteValue(hKey, ByVal StrPtr(Key$))
         End If
         RegCloseKey hKey
         Function = RetCode
    End Function
     
    Function GetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String) As String
    On Error Resume Next
    Dim RetCode As Long
    Dim hKey As Long
    Dim KeyNameA As AsciiZ * 256
    Local zTmp As AsciiZ * 256
    Dim acMainKey As AsciiZ * 300
    Local ZZZ As String
         acMainKey = cMainKey
         RetCode = RegOpenKeyEx(lpKey, acMainkey, 0&, %KEY_ALL_ACCESS, hKey)
         If RetCode = %ERROR_SUCCESS Then
           If Key$ = "*" Then Key$ = Chr$(0,0)
           szdat&=256
           Dim zbuffer As AsciiZ*256
           KeyNameA = Key
           cbData& = SIZEOF(zTmp)
           RetCode = RegQueryValueEx(ByVal hKey, KeyNameA, ByVal 0, KeyType&, zTmp, cbData&)
           ZZZ = zTmp
           Function = ZZZ
           Exit Function
         End If
         Function = ""
    End Function
     
    Function SetRegValue(lpKey As Long,ByVal cMainkey As String, ByVal Key As String, ByVal Setting As String) As Long
    On Error Resume Next
    Local hKey   As Long
    Local Result As Long
    Local zText  As AsciiZ * 2048
    If Key$ = "*" Then Key$ = Chr$(0,0)
    If RegCreateKeyEx(lpKey, cMainKey + Chr$(0),0, "", %REG_OPTION_NON_VOLATILE, _
                       %KEY_ALL_ACCESS, ByVal %NULL, hKey, Result) <> %ERROR_SUCCESS Then
        Function = 0
        Exit Function
    End If
    zText = Setting
    If Len(Setting) Then
        RegSetValueEx hKey, Key+Chr$(0), 0, %REG_SZ, zText, Len(Setting)+1
    Else
        RegSetValueEx hKey, Key+Chr$(0), 0, %REG_SZ, zText, 1
    End If
    RegCloseKey hKey
    Function = 0
    End Function
     
    Function DelRegKey(lpKey As Long, Key As String) Export As Long
    On Error Resume Next
    Local zStrKey As AsciiZ * 255, hKey As Long, Result As Long
    zStrKey = Key
    If RegOpenKeyEx(ByVal lpKey,zStrKey,0,  %KEY_ALL_ACCESS,  hKey) <> %ERROR_SUCCESS Then  Exit Function
    Result = RegDeleteKey(lpKey, zStrKey)
    RegCloseKey hKey
    Function = Result
    End Function

    [This message has been edited by Wayne Diamond (edited March 01, 2001).]
    -

  • #2
    Example usage
    Code:
    #COMPILE EXE  'compiles to just 8704 bytes - registry.inc doesn't add much overhead
    #INCLUDE "win32api.inc"
    #INCLUDE "registry.inc"
     
    FUNCTION PBMAIN() AS LONG
    'Create new value in new key
     If SetRegValue(%HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest", "NewSetting", "Data in key") = 0 THEN
        Msgbox "Set registry setting!"
     Else
        Msgbox "Couldnt set registry setting!"
     End If
     
     'Read new value
     Dim RegData As String
     RegData = GetRegValue(%HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest", "NewSetting")
     Msgbox "Data = " & RegData
     
     'Delete the new value
     If DelRegValue(%HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest", "NewSetting") = 0 THEN
        Msgbox "Deleted the value!"
     Else
        Msgbox "Couldn't delete the value!"
     End If
     
     'Delete the new key
     If DelRegKey(%HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest") = 0 THEN
        Msgbox "Deleted the key!"
     Else
        Msgbox "Couldn't delete the key!"
     End If
    END FUNCTION
    Or in it's simplest usage form:
    Code:
    #COMPILE EXE "regtiny.exe"
    #INCLUDE "win32api.inc"
    #INCLUDE "registry.inc"
    FUNCTION PBMAIN() AS LONG
     SetRegValue %HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest", "NewSetting", "Data in key"  'write
     Msgbox "You added: " & GetRegValue(%HKEY_LOCAL_MACHINE, "SOFTWARE\RegistryTest", "NewSetting") 'read
    END FUNCTION

    ------------------


    [This message has been edited by Wayne Diamond (edited March 01, 2001).]
    -

    Comment

    Working...
    X