Results 1 to 1 of 1

Thread: [VB] Registry

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    [VB] Registry

    VB Code:
    1. Option Explicit
    2.  
    3. 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
    4.  
    5.     ' Reg Key Security Options...
    6.     Const READ_CONTROL = &H20000
    7.     Const KEY_QUERY_VALUE = &H1
    8.     Const KEY_SET_VALUE = &H2
    9.     Const KEY_CREATE_SUB_KEY = &H4
    10.     Const KEY_ENUMERATE_SUB_KEYS = &H8
    11.     Const KEY_NOTIFY = &H10
    12.     Const KEY_CREATE_LINK = &H20
    13.  
    14.     ' Reg Key ROOT Types...
    15.     Const HKEY_LOCAL_MACHINE = &H80000002
    16.     Const ERROR_SUCCESS = 0
    17.     Const REG_SZ = 1                         ' Unicode nul terminated string
    18.     Const REG_DWORD = 4                      ' 32-bit number
    19.  
    20.     Const gREGKEYSYSINFOLOC = "SOFTWARE\Microsoft\Shared Tools Location"
    21.     Const gREGVALSYSINFOLOC = "MSINFO"
    22.     Const gREGKEYSYSINFO = "SOFTWARE\Microsoft\Shared Tools\MSINFO"
    23.     Const gREGVALSYSINFO = "PATH"
    24.  
    25. Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
    26. Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
    27. Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
    28. Public Const HKEY_CLASSES_ROOT = &H80000000
    29. Public Const HKEY_CURRENT_USER = &H80000001
    30. Public Const HKEY_USERS = &H80000003
    31. Public Const HKEY_PERFORMANCE_DATA = &H80000004
    32.  
    33.  
    34. Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    35. Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    36. Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    37. Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    38.  
    39.  
    40.     Const HKEY_DYN_DATA = &H80000006
    41.     Const REG_BINARY = 3
    42.     Const REG_DWORD_BIG_ENDIAN = 5
    43.     Const REG_DWORD_LITTLE_ENDIAN = 4
    44.     Const REG_EXPAND_SZ = 2
    45.     Const REG_LINK = 6
    46.     Const REG_MULTI_SZ = 7
    47.     Const REG_NONE = 0
    48.     Const REG_RESOURCE_LIST = 8
    49.  
    50.  
    51. Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String)
    52.  Dim lResult As Long
    53.  Dim lValueType As Long
    54.  Dim strBuf As String
    55.  Dim lDataBufSize As Long
    56.  On Error GoTo 0
    57.  lResult = RegQueryValueEx(hKey, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
    58.  If lResult = ERROR_SUCCESS Then
    59.     If lValueType = REG_SZ Then
    60.        strBuf = String(lDataBufSize, " ")
    61.        lResult = RegQueryValueEx(hKey, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
    62.        If lResult = ERROR_SUCCESS Then
    63.           RegQueryStringValue = StripTerminator(strBuf)
    64.        End If
    65.     End If
    66.  End If
    67. End Function
    68.  
    69. Public Function GetString(ByVal hKey As Long, ByVal strpath As String, ByVal strvalue As String)
    70.  Dim keyhand&
    71.  Dim datatype&, r#
    72.  r = RegOpenKey(hKey, strpath, keyhand&)
    73.  GetString = RegQueryStringValue(keyhand&, strvalue)
    74.  r = RegCloseKey(keyhand&)
    75. End Function
    76.  
    77. Function StripTerminator(ByVal strString As String) As String
    78.  Dim intZeroPos As Integer
    79.  intZeroPos = InStr(strString, Chr$(0))
    80.  If intZeroPos > 0 Then
    81.     StripTerminator = Left$(strString, intZeroPos - 1)
    82.  Else
    83.     StripTerminator = strString
    84.  End If
    85. End Function
    86.  
    87. Public Sub savestring(ByVal hKey As Long, ByVal strpath As String, ByVal strvalue As String, ByVal strdata As String)
    88.  Dim keyhand&, r#
    89.  r = RegCreateKey(hKey, strpath, keyhand&)
    90.  r = RegSetValueEx(keyhand&, strvalue, 0, REG_SZ, ByVal strdata, Len(strdata))
    91.  r = RegCloseKey(keyhand&)
    92. End Sub
    93.  
    94. Public Sub Delstring(ByVal hKey As Long, ByVal strpath As String, ByVal sKey As String)
    95.  Dim keyhand&, r#
    96.  r = RegOpenKey(hKey, strpath, keyhand&)
    97.  r = RegDeleteValue(keyhand&, sKey)
    98.  r = RegCloseKey(keyhand&)
    99. End Sub
    100.  
    101. Public Sub DeleteKey(ByVal hKey As Long, ByVal strpath As String, ByVal sKey As String)
    102.  Dim keyhand&, r#
    103.  r = RegOpenKey(hKey, strpath, keyhand&)
    104.  r = RegDeleteKey(keyhand&, sKey)
    105.  r = RegCloseKey(keyhand&)
    106. End Sub
    107.  
    108. Public Sub SaveSet(ByVal AppName As String, ByVal Section As String, ByVal Key As Variant, ByVal Value As Variant)
    109.     On Error Resume Next
    110.     savestring HKEY_CURRENT_USER, "Software\" & App.CompanyName & "\" & AppName & "\" & Section, CStr(Key), CStr(Value)
    111. End Sub
    112.  
    113. Public Function GetSet(ByVal AppName As String, ByVal Section As String, Key As Variant, Optional Default As Variant = "") As Variant
    114.     Dim hKey As Long, subkey As String, stringbuffer As String
    115.     Dim datatype As Long, slength As Long, retval As Long
    116.    
    117.     subkey = "Software\" & App.CompanyName & "\" & AppName & "\" & Section
    118.     retval = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_READ, hKey)
    119.    
    120.     stringbuffer = Space(255)
    121.     slength = 255
    122.    
    123.     retval = RegQueryValueEx(hKey, Key, 0, datatype, ByVal stringbuffer, slength)
    124.     ' Only attempt to display the data if it is in fact a string.
    125.     GetSet = CStr(Left(stringbuffer, slength - 1))
    126.     GetSet = Replace(CStr(Left(stringbuffer, slength - 1)), " ", "")
    127.     If Len(GetSet) > 0 Then
    128.         GetSet = CStr(Left(stringbuffer, slength - 1))
    129.     End If
    130.        
    131.    
    132.     If GetSet = "" Then GetSet = Default
    133.     ' Close the registry key.
    134.     retval = RegCloseKey(hKey)
    135.  
    136. End Function
    137.  
    138. Public Function DelSet(ByVal AppName As String, ByVal Section As String, Key As Variant) As Variant
    139.     On Error Resume Next
    140.     Delstring HKEY_CURRENT_USER, "Software\" & App.CompanyName & "\" & AppName & "\" & Section, CStr(Key)
    141. End Function
    142.  
    143. Public Function DelKey(ByVal AppName As String, ByVal Section As String, Key As Variant) As Variant
    144.     On Error Resume Next
    145.     DeleteKey HKEY_CURRENT_USER, "Software\" & App.CompanyName & "\" & AppName & "\" & Section, CStr(Key)
    146. End Function
    Last edited by Evan; Apr 14th, 2004 at 07:18 PM.

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