' API declarations and simple wrapper functions for the Registry
'
'
'
' JET 2 engine required, no known JET 3 features used
'
'
' Modified:
' AD 4/ 9/95 Initial version
'
Option Explicit
#If Win16 Then
' ERROR - This code does not work under 16 bit Windows
' The Registry does exist from W3.1 onwards,
' but only for use with OLE registration. This
' code does not attempt to support these functions
#End If
#If Win32 Then
#Else
' ERROR - This code requires 32 bit Windows
#End If
' NB - We can't nest a #Const within a conditional compile block
' #If Win32 Then
' Define this true to use '95 style enhanced functions with access control (RegOpenKeyEx etc.)
' False uses the 32 bit version of the 16 bit functions
#Const UseRegExFunctions = True
' #Else
' For 16 bits, these functions will simply be unavailable
' #Const UseRegExFunctions = False
' #End If
'Registration functions
#If Win16 Then
' Old 16 bit functions
Public Declare Function RegCloseKey Lib "SHELL.DLL" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, lphKey As Long) As Long
Public Declare Function RegDeleteKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String) As Long
Public Declare Function RegEnumKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal iSubKey As Long, ByVal lpszBuffer As String, ByVal cbBuffer As Long) As Long
Public Declare Function RegOpenKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, lphkResult As Long) As Long
Public Declare Function RegQueryValue Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal lpszValue As String, lpcbValue As Long) As Long
Public Declare Function RegSetValue Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal fdwType As Long, ByVal lpszValue As String, ByVal dwLength As Long) As Long
#End If
#If Win32 Then
' 32 bit declares for Win32s
' NB - Don't use the supplied WIN32API.TXT instead. It has bugs in the translation
' of its function headers from C to VB.
' RegEnumValue & RegSetValue (et al) have lpData declared "lpData As Byte", when it needs to be "lpData As Any"
' if we are to manipulate pointers to arrays of bytes from VB
' These declares will be qualified as "Private" as each new wrapper function is implemented
' Exception - we won't wrap these two, as their headers are trivially compatible with VB
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegDeleteKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegEnumKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
'Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegEnumValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
'Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Private Declare Function RegOpenKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegOpenKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
'Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegQueryValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
Private Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExA Lib "advapi32.dll" (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 Declare Function GetEnvironmentStrings Lib "Kernel32" Alias "GetEnvironmentStringsA" () As String
Public Declare Function GetEnvironmentVariable Lib "Kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Declare Function SetEnvironmentVariable Lib "Kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Public Declare Function ExpandEnvironmentStrings Lib "Kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nDstSize As Long) As Long
#End If
' Return codes from Registration functions
Public Const ERROR_SUCCESS = 0&
#If Win16 Then
' These constants are taken from 16-bit sources
' They appear to be incompatible with the 32-bit constants
Public Const ERROR_BADDB = 1&
Public Const ERROR_BADKEY = 2&
Public Const ERROR_CANTOPEN = 3&
Public Const ERROR_CANTREAD = 4&
Public Const ERROR_CANTWRITE = 5&
#End If
Public Const ERROR_OUTOFMEMORY = 6&
Public Const ERROR_INVALID_PARAMETER = 7&
Public Const ERROR_ACCESS_DENIED = 8&
#If Win32 Then
' More data is available.
Public Const ERROR_MORE_DATA = 234 ' dderror
' The configuration registry database is corrupt.
Public Const ERROR_BADDB = 1009&
' The configuration registry key is invalid.
Public Const ERROR_BADKEY = 1010&
' The configuration registry key could not be opened.
Public Const ERROR_CANTOPEN = 1011&
' The configuration registry key could not be read.
Public Const ERROR_CANTREAD = 1012&
' The configuration registry key could not be written.
Public Const ERROR_CANTWRITE = 1013&
' One of the files in the Registry database had to be recovered
' by use of a log or alternate copy. The recovery was successful.
Public Const ERROR_REGISTRY_RECOVERED = 1014&
' The Registry is corrupt. The structure of one of the files that contains
' Registry data is corrupt, or the system's image of the file in memory
' is corrupt, or the file could not be recovered because the alternate
' copy or log was absent or corrupt.
Public Const ERROR_REGISTRY_CORRUPT = 1015&
' An I/O operation initiated by the Registry failed unrecoverably.
' The Registry could not read in, or write out, or flush, one of the files
' that contain the system's image of the Registry.
Public Const ERROR_REGISTRY_IO_FAILED = 1016&
' The system has attempted to load or restore a file into the Registry, but the
' specified file is not in a Registry file format.
Public Const ERROR_NOT_REGISTRY_FILE = 1017&
' Illegal operation attempted on a Registry key which has been marked for deletion.
Public Const ERROR_KEY_DELETED = 1018&
' System could not allocate the required space in a Registry log.
Public Const ERROR_NO_LOG_SPACE = 1019&
' Cannot create a symbolic link in a Registry key that already
' has subkeys or values.
Public Const ERROR_KEY_HAS_CHILDREN = 1020&
' Cannot create a stable subkey under a volatile parent key.
Public Const ERROR_CHILD_MUST_BE_VOLATILE = 1021&
#End If
#If Win32 Then
' These are the generic rights.
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_EXECUTE = &H20000000
Public Const GENERIC_ALL = &H10000000
#End If
'Registration constants
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_READ = READ_CONTROL
Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Public Const STANDARD_RIGHTS_ALL = &H1F0000
'//
'// Registry Specific Access Rights.
'//
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY) _
And _
(Not SYNCHRONIZE))
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or _
KEY_SET_VALUE Or _
KEY_CREATE_SUB_KEY) _
And _
(Not SYNCHRONIZE))
Public Const KEY_EXECUTE = ((KEY_READ) _
And _
(Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _