Monitor Changes to the Registry 'PUT INTO .BAS MODULEPublic Enum ROOT_KEYS HKEY_CLASSES_ROOT = &H80000000 HKEY_CURRENT_USER = &H80000001 HKEY_LOCAL_MACHINE = &H80000002 HKEY_USERS = &H80000003 HKEY_PERFORMANCE_DATA = &H80000004 HKEY_CURRENT_CONFIG = &H80000005 HKEY_DYN_DATA = &H80000006 End EnumPublic Enum NOTIFY_EVENTS REG_NOTIFY_CHANGE_NAME = &H1 REG_NOTIFY_CHANGE_ATTRIBUTES = &H2 REG_NOTIFY_CHANGE_LAST_SET = &H4 REG_NOTIFY_CHANGE_SECURITY = &H8 End EnumPrivate Declare Function RegNotifyChangeKeyValue Lib "advapi32" _ (ByVal hKey As Long, ByVal bWatchSubTree As Boolean, ByVal _ dwNotifyFilter As Long, ByVal hEvent As Long, ByVal _ fAsynchronous As Boolean) As LongPrivate Declare Function RegOpenKey Lib "advapi32.dll" _ Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _ String, phkResult As Long) As LongPrivate Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _ hKey As Long) As Long'PUT INTO .BAS MODULEPublic Sub RegMonitor(hKey As ROOT_KEYS, sRegKeyPath As String, _ bWatchSubTree As Boolean, dwFilters As NOTIFY_EVENTS) Dim lKeyHandle As Long, lRet As Long lRet = RegOpenKey(hKey, sRegKeyPath, lKeyHandle) RegNotifyChangeKeyValue lKeyHandle, bWatchSubTree, _ dwFilters, 0&, False lRet = RegCloseKey(lKeyHandle) End Sub'---Using--- Private Sub Command1_Click() Dim cap As String sChange = "" cap = Caption Caption = "Monitoring..." Label1 = "Try to change HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Registry Notification\Hello\Testing" Label1 = Label1 & vbCrLf & "or any other key under HKCU\Software\VB and VBA Program Settings with RegEdit" Call RegMonitor(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings", True, REG_NOTIFY_CHANGE_ATTRIBUTES + REG_NOTIFY_CHANGE_LAST_SET + REG_NOTIFY_CHANGE_NAME + REG_NOTIFY_CHANGE_SECURITY) ' Try to change this key or any other under HKCU Label1 = "Your key have been changed!!!" Label1 = Label1 & vbCrLf & "Press command button to start monitoring. (Your App will freeze while monitoring)" Caption = cap End SubPrivate Sub Form_Load() Label1 = "Application created a temporary registry key:" Label1 = Label1 & vbCrLf & "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Registry Notification\Hello\Testing" Label1 = Label1 & vbCrLf & "Press command button to start monitoring. (Your App will freeze while monitoring)" 'Create a temporary registry key SaveSetting "Registry Notification", "Hello", "Testing", "123" End Sub
[email protected]
'PUT INTO .BAS MODULEPublic Enum ROOT_KEYS
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_CURRENT_CONFIG = &H80000005
HKEY_DYN_DATA = &H80000006
End EnumPublic Enum NOTIFY_EVENTS
REG_NOTIFY_CHANGE_NAME = &H1
REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
REG_NOTIFY_CHANGE_LAST_SET = &H4
REG_NOTIFY_CHANGE_SECURITY = &H8
End EnumPrivate Declare Function RegNotifyChangeKeyValue Lib "advapi32" _
(ByVal hKey As Long, ByVal bWatchSubTree As Boolean, ByVal _
dwNotifyFilter As Long, ByVal hEvent As Long, ByVal _
fAsynchronous As Boolean) As LongPrivate Declare Function RegOpenKey Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As LongPrivate Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long'PUT INTO .BAS MODULEPublic Sub RegMonitor(hKey As ROOT_KEYS, sRegKeyPath As String, _
bWatchSubTree As Boolean, dwFilters As NOTIFY_EVENTS) Dim lKeyHandle As Long, lRet As Long
lRet = RegOpenKey(hKey, sRegKeyPath, lKeyHandle)
RegNotifyChangeKeyValue lKeyHandle, bWatchSubTree, _
dwFilters, 0&, False
lRet = RegCloseKey(lKeyHandle)
End Sub'---Using---
Private Sub Command1_Click()
Dim cap As String
sChange = ""
cap = Caption
Caption = "Monitoring..."
Label1 = "Try to change HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Registry Notification\Hello\Testing"
Label1 = Label1 & vbCrLf & "or any other key under HKCU\Software\VB and VBA Program Settings with RegEdit"
Call RegMonitor(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings", True, REG_NOTIFY_CHANGE_ATTRIBUTES + REG_NOTIFY_CHANGE_LAST_SET + REG_NOTIFY_CHANGE_NAME + REG_NOTIFY_CHANGE_SECURITY)
' Try to change this key or any other under HKCU
Label1 = "Your key have been changed!!!"
Label1 = Label1 & vbCrLf & "Press command button to start monitoring. (Your App will freeze while monitoring)"
Caption = cap
End SubPrivate Sub Form_Load()
Label1 = "Application created a temporary registry key:"
Label1 = Label1 & vbCrLf & "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Registry Notification\Hello\Testing"
Label1 = Label1 & vbCrLf & "Press command button to start monitoring. (Your App will freeze while monitoring)"
'Create a temporary registry key
SaveSetting "Registry Notification", "Hello", "Testing", "123"
End Sub