Attribute VB_Name = "basRegistry" Option Explicit ' Hkey cache (used for logging purposes) Private Type HKEY_CACHE hKey As Long strHkey As String End Type Private hkeyCache() As HKEY_CACHE 'Registry security options Public Const READ_CONTROL = &H20000 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_ALL_ACCESS = &H3F Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _ KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _ KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL 'Registry root keys Public Const HKEY_CLASSES_ROOT = &H80000000 Public Const HKEY_CURRENT_USER = &H80000001 Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const HKEY_USERS = &H80000003 Public Const HKEY_PERFORMANCE_DATA = &H80000004 Public Const HKEY_CURRENT_CONFIG = &H80000005 Public Const HKEY_DYN_DATA = &H80000006 'Registry API return value Public Const ERROR_SUCCESS = 0 Public Const ERROR_FAIL = -1 Public Const ERROR_NONE = 0 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 Public Const ERROR_OUTOFMEMORY = 6 'Public Const ERROR_INVALID_PARAMETER = 7 'old Public Const ERROR_ARENA_TRASHED = 7 Public Const ERROR_ACCESS_DENIED = 8 Public Const ERROR_INVALID_PARAMETERS = 87 Public Const ERROR_NO_MORE_ITEMS = 259 'Registry options Public Const REG_OPTION_NON_VOLATILE = 0 'Registry datatypes Public Const REG_NONE As Long = 0 ' No value type Public Const REG_SZ As Long = 1 ' Unicode nul terminated string Public Const REG_EXPAND_SZ As Long = 2 ' Unicode nul terminated string (with environment variable references) Public Const REG_BINARY As Long = 3 ' Free form binary Public Const REG_DWORD As Long = 4 ' 32-bit number Public Const REG_DWORD_BIG_ENDIAN As Long = 5 ' 32-bit number Public Const REG_LINK As Long = 6 ' Symbolic Link (unicode) Public Const REG_MULTI_SZ As Long = 7 ' Multiple Unicode strings Public Const REG_RESOURCE_LIST As Long = 8 ' Resource list in the resource map Public Const REG_FULL_RESOURCE_DESCRIPTOR As Long = 9 ' Resource list in the hardware description Public Const REG_RESOURCE_REQUIREMENTS_LIST As Long = 10 Public Const REG_QWORD As Long = 11 ' 64-bit number 'Primary registry location for SysInfo application Public Const kRegKeySysInfoKey As String = "SOFTWARE\Microsoft\Shared Tools\MSINFO" Public Const kRegKeySysInfoSubKey As String = "PATH" 'Secondary registry location for SysInfo application Public Const kRegKeySysInfoSecondaryKey As String = "SOFTWARE\Microsoft\Shared Tools Location" Public Const kRegValueSysInfoSecondarySubKey As String = "MSINFO" 'Registry API declarations Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal szSubKey As String, hkeyResult As Long) As Long 'Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long Public Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal szSubKey As String, ByVal szValue As String, chValue As Long) As Long Public Declare Function RegQueryValueEx Lib "advapi32.dll" 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 Public Declare Function RegQueryValueExString 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 Public Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long Public Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal szSubKey As String, hkeyResult As Long) As Long Public 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal szSubKey As String) As Long Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal iSubKey As Long, ByVal szBuffer As String, ByVal cbBuf As Long) As Long Public Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal szSubKey As String, ByVal fdwType As Long, ByVal lpszValue As String, ByVal cb As Long) As Long Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As Any, ByVal cbData As Long) As Long Public Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long Public Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long Public Declare Function GetCurrentProcessId Lib "kernel32.dll" () As Long Public Function CheckKey(ByVal lhKey As Long, sKeyName As String) As Long Dim lRetVal As Long 'result of the API functions Dim hKey As Long 'handle of opened key lRetVal = RegOpenKeyEx(lhKey, sKeyName, 0, KEY_QUERY_VALUE, hKey) If (lRetVal = ERROR_NONE) Then RegCloseKey (hKey) CheckKey = lRetVal End Function Public Sub CreateNewKey(ByVal hKey As Long, sNewKeyName As String) Dim hNewKey As Long 'handle to the new key Dim lRetVal As Long 'result of the RegCreateKeyEx function lRetVal = RegCreateKeyEx(hKey, sNewKeyName, 0&, vbNullString, _ REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal) RegCloseKey (hNewKey) End Sub Public Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long Dim lValue As Long Dim sValue As String Select Case lType Case REG_SZ sValue = vValue & Chr$(0) SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue)) Case REG_DWORD lValue = vValue SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4) End Select End Function Public Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _ String, vValue As Variant) As Long Dim cch As Long Dim lrc As Long Dim lType As Long Dim lValue As Long Dim sValue As String On Error GoTo QueryValueExError ' Determine the size and type of data to be read lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch) If lrc <> ERROR_NONE Then Error 5 Select Case lType Case REG_SZ: ' For strings sValue = String(cch, 0) lrc = RegQueryValueExString(lhKey, szValueName, 0&, _ lType, sValue, cch) If lrc = ERROR_NONE Then vValue = Left$(sValue, cch - 1) Else vValue = Empty End If ' For DWORDS Case REG_DWORD: lrc = RegQueryValueExLong(lhKey, szValueName, 0&, _ lType, lValue, cch) If lrc = ERROR_NONE Then vValue = lValue Case Else 'all other data types not supported lrc = -1 End Select QueryValueExExit: QueryValueEx = lrc Exit Function QueryValueExError: Resume QueryValueExExit End Function Public Function QueryValue(ByVal lhKey As Long, sKeyName As String, sValueName As String, vValue As Variant) As Long Dim lRetVal As Long 'result of the API functions Dim hKey As Long 'handle of opened key 'Dim vValue As Variant 'setting of queried value lRetVal = RegOpenKeyEx(lhKey, sKeyName, 0, KEY_QUERY_VALUE, hKey) lRetVal = QueryValueEx(hKey, sValueName, vValue) RegCloseKey (hKey) QueryValue = lRetVal End Function Public Sub SetKeyValue(ByVal lhKey As Long, sKeyName As String, sValueName As String, _ vValueSetting As Variant, lValueType As Long) Dim lRetVal As Long 'result of the SetValueEx function Dim hKey As Long 'handle of open key 'open the specified key lRetVal = RegOpenKeyEx(lhKey, sKeyName, 0, _ KEY_SET_VALUE, hKey) lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting) RegCloseKey (hKey) End Sub 'return HKEY text string representing root key Public Function strGetPredefinedHKEYString(ByVal hKey As Long) As String Select Case hKey Case HKEY_CLASSES_ROOT strGetPredefinedHKEYString = "HKEY_CLASSES_ROOT" Case HKEY_CURRENT_USER strGetPredefinedHKEYString = "HKEY_CURRENT_USER" Case HKEY_LOCAL_MACHINE strGetPredefinedHKEYString = "HKEY_LOCAL_MACHINE" Case HKEY_USERS strGetPredefinedHKEYString = "HKEY_USERS" End Select End Function ' Delete a key Public Sub DeleteKey(ByVal lhKey As Long, sParentKey As String, sKeyName As String) Dim hKey As Long 'Open the parent key If RegOpenKeyEx(lhKey, sParentKey, 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then Exit Sub 'Error opening parentkey End If If (sKeyName = "") Then Exit Sub RegDeleteKey hKey, sKeyName ' Delete the subkey. RegCloseKey hKey ' Close the key. End Sub