This topic show you how to gain access to the complete registry.

Microsoft Visual Basic 5.0Microsoft Visual Basic 4.0 32-bit

This document has been viewed times since February 22, 1998.
Last modified on February 25, 1998.

'At top level of a module, always include to be sure that all variables have the right type
Option Explicit

'Then declare this constant. csProgramSection holds base key for your applications settings
Private Const csProgramSection As String = "Software\Your Name\Your App\"

'Then declare these functions for settings and retreiving information from the System registry
Private Declare Function RegOpenKey Lib "AdvAPI32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "AdvAPI32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "AdvAPI32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "AdvAPI32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "AdvAPI32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegFlushKey Lib "AdvAPI32.dll" (ByVal hKey As Long) As Long

'Then all we have to do is to write a public function like this...
Public Function ReadRegistry(ByVal Section As String, ByVal Key As String) As String

'Declare error check varaible lResult, key handling variables lKeyValue, lDataTypeValue, lValueLength and sValue
Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String

'Turn on error trapping
On Error Resume Next

'Now open the selected base key from the registry
lResult = RegOpenKey(HKEY_CURRENT_USER, sProgramSection & Section, lKeyValue)

'Initiate an empty variable sValue to hold retreived information to a maximum of 2048 bytes
sValue = Space$(2048)
lValueLength = Len(sValue)

'Now open the selected key under the base key
lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)

'Check if an error occured meanwhile. If so return a default empty string
If (lResult = 0) And (Err.Number = 0) Then
    sValue = Left$(sValue, lValueLength - 1)
Else
    sValue = Empty
End If

'Now close the opened key and return the value to the function
lResult = RegCloseKey(lKeyValue)
ReadRegistry = sValue

End Function

'Now we shall be able to write to the registry, not only read...
Public Sub WriteRegistry(ByVal Section As String, ByVal Key As String, ByVal Value As String)

'Declare error check varaible lResult and key handling variable lKeyValue
Dim lResult As Long, lKeyValue As Long

'Turn on error trapping
On Error Resume Next

'If the key we wish to write do not exist, create it
lResult = RegCreateKey(HKEY_CURRENT_USER, sProgramSection & Section, lKeyValue)

'Now set value in the registry
lResult = RegSetValueEx(lKeyValue, Key, 0&, 1&, Value, Len(Value))

'And finally, flush the registry buffer and clode the key
lResult = RegFlushKey(lKeyValue)
lResult = RegCloseKey(lKeyValue)

End Sub

Please email me suggestions, improvements or anything else you want to be included on my pages.