Results 1 to 4 of 4

Thread: Writing to and Reading from the registry

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    41

    Cool Writing to and Reading from the registry

    Hi,
    I am writing a piece of Security software to replace the glitchy, crappy windows policy editor (poledit). It is only when I come to writing the code for the policys that I realise that the 'GetSetting' and 'WriteSetting' function that I realise it can only be used to write to 'HKEY_CURRENT_USER/Software/VB/product/'. Could someone please recommend a way of writing to the registry wherever I want.

    Thanks for any replies in advanced
    Regards
    S Roberts

  2. #2
    Stiletto
    Guest
    You will need to use the API function for it

  3. #3
    Stiletto
    Guest
    Add a new module to your project, and insert this in it:
    VB Code:
    1. Option Explicit
    2. Public Type FILETIME
    3.   dwLowDateTime As Long
    4.   dwHighDateTime As Long
    5. End Type
    6.  
    7. Public Declare Function RegCloseKey _
    8.   Lib "advapi32.dll" _
    9.   (ByVal lngHKey As Long) _
    10.   As Long
    11.  
    12. Public Declare Function RegCreateKeyEx _
    13.   Lib "advapi32.dll" _
    14.   Alias "RegCreateKeyExA" _
    15.   (ByVal lngHKey As Long, _
    16.     ByVal lpSubKey As String, _
    17.     ByVal Reserved As Long, _
    18.     ByVal lpClass As String, _
    19.     ByVal dwOptions As Long, _
    20.     ByVal samDesired As Long, _
    21.     ByVal lpSecurityAttributes As Long, _
    22.     phkResult As Long, _
    23.     lpdwDisposition As Long) _
    24.   As Long
    25.  
    26. Public Declare Function RegOpenKeyEx _
    27.   Lib "advapi32.dll" _
    28.   Alias "RegOpenKeyExA" _
    29.   (ByVal lngHKey As Long, _
    30.     ByVal lpSubKey As String, _
    31.     ByVal ulOptions As Long, _
    32.     ByVal samDesired As Long, _
    33.     phkResult As Long) _
    34.   As Long
    35.  
    36. Public Declare Function RegQueryValueExString _
    37.   Lib "advapi32.dll" _
    38.   Alias "RegQueryValueExA" _
    39.   (ByVal lngHKey As Long, _
    40.     ByVal lpValueName As String, _
    41.     ByVal lpReserved As Long, _
    42.     lpType As Long, _
    43.     ByVal lpData As String, _
    44.     lpcbData As Long) _
    45.   As Long
    46.  
    47. Public Declare Function RegQueryValueExLong _
    48.   Lib "advapi32.dll" _
    49.   Alias "RegQueryValueExA" _
    50.   (ByVal lngHKey As Long, _
    51.     ByVal lpValueName As String, _
    52.     ByVal lpReserved As Long, _
    53.     lpType As Long, _
    54.     lpData As Long, _
    55.     lpcbData As Long) _
    56.   As Long
    57.  
    58. Public Declare Function RegQueryValueExBinary _
    59.   Lib "advapi32.dll" _
    60.   Alias "RegQueryValueExA" _
    61.   (ByVal lngHKey As Long, _
    62.     ByVal lpValueName As String, _
    63.     ByVal lpReserved As Long, _
    64.     lpType As Long, _
    65.     ByVal lpData As Long, _
    66.     lpcbData As Long) _
    67.   As Long
    68.  
    69. Public Declare Function RegQueryValueExNULL _
    70.   Lib "advapi32.dll" _
    71.   Alias "RegQueryValueExA" _
    72.   (ByVal lngHKey As Long, _
    73.     ByVal lpValueName As String, _
    74.     ByVal lpReserved As Long, _
    75.     lpType As Long, _
    76.     ByVal lpData As Long, _
    77.     lpcbData As Long) _
    78.   As Long
    79.  
    80. Public Declare Function RegSetValueExString _
    81.   Lib "advapi32.dll" _
    82.   Alias "RegSetValueExA" _
    83.   (ByVal lngHKey As Long, _
    84.     ByVal lpValueName As String, _
    85.     ByVal Reserved As Long, _
    86.     ByVal dwType As Long, _
    87.     ByVal lpValue As String, _
    88.     ByVal cbData As Long) _
    89.   As Long
    90.  
    91. Public Declare Function RegSetValueExLong _
    92.   Lib "advapi32.dll" _
    93.   Alias "RegSetValueExA" _
    94.   (ByVal lngHKey As Long, _
    95.     ByVal lpValueName As String, _
    96.     ByVal Reserved As Long, _
    97.     ByVal dwType As Long, _
    98.     lpValue As Long, _
    99.     ByVal cbData As Long) _
    100.   As Long
    101.  
    102. Public Declare Function RegSetValueExBinary _
    103.   Lib "advapi32.dll" _
    104.   Alias "RegSetValueExA" _
    105.   (ByVal lngHKey As Long, _
    106.     ByVal lpValueName As String, _
    107.     ByVal Reserved As Long, _
    108.     ByVal dwType As Long, _
    109.     ByVal lpValue As Long, _
    110.     ByVal cbData As Long) _
    111.   As Long
    112.  
    113. Public Declare Function RegEnumKey _
    114.   Lib "advapi32.dll" _
    115.   Alias "RegEnumKeyA" _
    116.   (ByVal lngHKey As Long, _
    117.     ByVal dwIndex As Long, _
    118.     ByVal lpName As String, _
    119.     ByVal cbName As Long) _
    120.   As Long
    121.  
    122. Public Declare Function RegQueryInfoKey _
    123.   Lib "advapi32.dll" _
    124.   Alias "RegQueryInfoKeyA" _
    125.   (ByVal lngHKey As Long, _
    126.     ByVal lpClass As String, _
    127.     ByVal lpcbClass As Long, _
    128.     ByVal lpReserved As Long, _
    129.     lpcSubKeys As Long, _
    130.     lpcbMaxSubKeyLen As Long, _
    131.     ByVal lpcbMaxClassLen As Long, _
    132.     lpcValues As Long, _
    133.     lpcbMaxValueNameLen As Long, _
    134.     ByVal lpcbMaxValueLen As Long, _
    135.     ByVal lpcbSecurityDescriptor As Long, _
    136.     lpftLastWriteTime As FILETIME) _
    137.   As Long
    138.  
    139. Public Declare Function RegEnumValue _
    140.   Lib "advapi32.dll" _
    141.   Alias "RegEnumValueA" _
    142.   (ByVal lngHKey As Long, _
    143.     ByVal dwIndex As Long, _
    144.     ByVal lpValueName As String, _
    145.     lpcbValueName As Long, _
    146.     ByVal lpReserved As Long, _
    147.     ByVal lpType As Long, _
    148.     ByVal lpData As Byte, _
    149.     ByVal lpcbData As Long) _
    150.   As Long
    151.  
    152. Public Declare Function RegDeleteKey _
    153.   Lib "advapi32.dll" _
    154.   Alias "RegDeleteKeyA" _
    155.   (ByVal lngHKey As Long, _
    156.     ByVal lpSubKey As String) _
    157.   As Long
    158.  
    159. Public Declare Function RegDeleteValue _
    160.   Lib "advapi32.dll" _
    161.   Alias "RegDeleteValueA" _
    162.   (ByVal lngHKey As Long, _
    163.     ByVal lpValueName As String) _
    164.   As Long
    165.  
    166. Public Enum EnumRegistryRootKeys
    167.   rrkHKeyClassesRoot = &H80000000
    168.   rrkHKeyCurrentUser = &H80000001
    169.   rrkHKeyLocalMachine = &H80000002
    170.   rrkHKeyUsers = &H80000003
    171. End Enum
    172.  
    173. Public Enum EnumRegistryValueType
    174.   rrkRegSZ = 1
    175.   rrkregBinary = 3
    176.   rrkRegDWord = 4
    177. End Enum
    178.  
    179. Public Const mcregOptionNonVolatile = 0
    180.  
    181. Public Const mcregErrorNone = 0
    182. Public Const mcregErrorBadDB = 1
    183. Public Const mcregErrorBadKey = 2
    184. Public Const mcregErrorCantOpen = 3
    185. Public Const mcregErrorCantRead = 4
    186. Public Const mcregErrorCantWrite = 5
    187. Public Const mcregErrorOutOfMemory = 6
    188. Public Const mcregErrorInvalidParameter = 7
    189. Public Const mcregErrorAccessDenied = 8
    190. Public Const mcregErrorInvalidParameterS = 87
    191. Public Const mcregErrorNoMoreItems = 259
    192.  
    193. Public Const mcregKeyAllAccess = &H3F
    194. Public Const mcregKeyQueryValue = &H1
    195.  
    196. Public Function RegistryGetKeyValue( _
    197.   eRootKey As EnumRegistryRootKeys, _
    198.   strKeyName As String, _
    199.   strValueName As String) _
    200.   As Variant
    201.   ' Comments  : Returns a value from the system registry
    202.   ' Parameters: eRootKey - The root key
    203.   '             strKeyName - The name of the key
    204.   '             strValueName - The name of the value
    205.   ' Returns   : The data in the registry value
    206.   '
    207.   Dim lngRetVal As Long
    208.   Dim lngHKey As Long
    209.   Dim varValue As Variant
    210.   Dim strValueData As String
    211.   Dim abytValueData() As Byte
    212.   Dim lngValueData As Long
    213.   Dim lngValueType As Long
    214.   Dim lngDataSize As Long
    215.  
    216.   On Error GoTo PROC_ERR
    217.  
    218.   varValue = Empty
    219.  
    220.   lngRetVal = RegOpenKeyEx(eRootKey, strKeyName, 0&, mcregKeyQueryValue, _
    221.     lngHKey)
    222.  
    223.   If mcregErrorNone = lngRetVal Then
    224.    
    225.     lngRetVal = RegQueryValueExNULL(lngHKey, strValueName, 0&, lngValueType, _
    226.       0&, lngDataSize)
    227.    
    228.     If lngRetVal = mcregErrorNone Then
    229.      
    230.       Select Case lngValueType
    231.      
    232.       ' String type
    233.  
    234.         Case rrkRegSZ:
    235.           If lngDataSize > 0 Then
    236.             strValueData = String(lngDataSize, 0)
    237.             lngRetVal = RegQueryValueExString(lngHKey, strValueName, 0&, _
    238.               lngValueType, strValueData, lngDataSize)
    239.             If InStr(strValueData, vbNullChar) > 0 Then
    240.               strValueData = Mid$(strValueData, 1, InStr(strValueData, _
    241.                 vbNullChar) - 1)
    242.             End If
    243.           End If
    244.           If mcregErrorNone = lngRetVal Then
    245.             varValue = Left$(strValueData, lngDataSize)
    246.           Else
    247.             varValue = Empty
    248.           End If
    249.        
    250.         ' Long type
    251.         Case rrkRegDWord:
    252.           lngRetVal = RegQueryValueExLong(lngHKey, strValueName, 0&, _
    253.             lngValueType, lngValueData, lngDataSize)
    254.           If mcregErrorNone = lngRetVal Then
    255.             varValue = lngValueData
    256.           End If
    257.                
    258.         ' Binary type
    259.         Case rrkregBinary
    260.           If lngDataSize > 0 Then
    261.             ReDim abytValueData(lngDataSize) As Byte
    262.             lngRetVal = RegQueryValueExBinary(lngHKey, strValueName, 0&, _
    263.               lngValueType, VarPtr(abytValueData(0)), lngDataSize)
    264.           End If
    265.           If mcregErrorNone = lngRetVal Then
    266.             varValue = abytValueData
    267.           Else
    268.             varValue = Empty
    269.           End If
    270.                
    271.         Case Else
    272.           'No other data types supported
    273.           lngRetVal = -1
    274.        
    275.       End Select
    276.      
    277.     End If
    278.    
    279.     RegCloseKey (lngHKey)
    280.   End If
    281.  
    282.   'Return varValue
    283.   RegistryGetKeyValue = varValue
    284. PROC_EXIT:
    285.   Exit Function
    286.  
    287. PROC_ERR:
    288.   MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
    289.     "RegistryGetKeyValue"
    290.   Resume PROC_EXIT
    291. End Function
    292.  
    293. Public Sub RegistrySetKeyValue( _
    294.   eRootKey As EnumRegistryRootKeys, _
    295.   strKeyName As String, _
    296.   strValueName As String, _
    297.   varData As Variant, _
    298.   eDataType As EnumRegistryValueType)
    299.   ' Comments  : This procedure sets a key value
    300.   ' Parameters: eRootKey - The root key
    301.   '             strKeyName - The name of the key
    302.   '             strValueName - The name of the value
    303.   '             varData - The data to store in the value
    304.   '             eDataType - The type of data to store in the value
    305.   ' Returns   : Nothing
    306.   '
    307.   Dim lngRetVal As Long
    308.   Dim lngHKey As Long
    309.   Dim strData As String
    310.   Dim lngData As Long
    311.   Dim abytData() As Byte
    312.    
    313.   On Error GoTo PROC_ERR
    314.  
    315.   ' Open the specified key, if it does not exist then create it
    316.   lngRetVal = RegCreateKeyEx(eRootKey, strKeyName, 0&, vbNullString, _
    317.     mcregOptionNonVolatile, mcregKeyAllAccess, 0&, lngHKey, 0&)
    318.  
    319.   ' Determine the data type of the key
    320.   Select Case eDataType
    321.  
    322.   Case rrkRegSZ
    323.     strData = varData & vbNullChar
    324.     lngRetVal = RegSetValueExString(lngHKey, strValueName, 0&, eDataType, _
    325.       strData, Len(strData))
    326.    
    327.   Case rrkRegDWord
    328.     lngData = varData
    329.     lngRetVal = RegSetValueExLong(lngHKey, strValueName, 0&, eDataType, _
    330.       lngData, Len(lngData))
    331.  
    332.   ' Binary type
    333.   Case rrkregBinary
    334.     abytData = varData
    335.     lngRetVal = RegSetValueExBinary(lngHKey, strValueName, 0&, eDataType, _
    336.       VarPtr(abytData(0)), UBound(abytData) + 1)
    337.  
    338.   End Select
    339.  
    340.   RegCloseKey (lngHKey)
    341.    
    342. PROC_EXIT:
    343.   Exit Sub
    344.  
    345. PROC_ERR:
    346.   MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
    347.     "RegistrySetKeyValue"
    348.   Resume PROC_EXIT
    349.    
    350. End Sub
    Usage:
    VB Code:
    1. 'Example of usage
    2. Call RegistrySetKeyValue(rrkHKeyCurrentUser, "Software\Microsoft...", "Pen Color", DecFore.Text, rrkRegDWord)
    Good luck

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    41

    Smile Thanks

    Thanks very much. Also I wonder if you could answer this question:

    Is there anyway that I can obtain a list of users that are use the machine and put them into an array

    Thanks
    S Roberts

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