这有一个监控注册表的例子,你自已改一改吧。
'This program shows you how to use the RegNotifyChangeKeyValue-function
'As its name predicts, RegNotifyChangeKeyValue will notify our program
'when the registry changes.
'Follow these steps to test this program:
'1. Start this program
'2. Start Regedit.exe (located in your Windows-directory)
'3. Go to HKEY_CURRENT_USER
'4. Change the value of HKEY_CURRENT_USER
'5. You will see that our program returns from the RegNotifyChangeKeyValue
'WARNING: Playing with the registry can have serious consequences !
' If you don't know what you're doing, I advise you not to delete anything
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_NOTIFY_CHANGE_NAME = &H1 ' Create or delete (child)
Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Const REG_NOTIFY_CHANGE_LAST_SET = &H4 ' time stamp
Const REG_NOTIFY_CHANGE_SECURITY = &H8
Const REG_NOTIFY_ALL = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
Private 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 Long
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'Create a temporary registry key
SaveSetting "Registry Notification", "Hello", "Testing", "123"
'Call the function .. This will notify us when something changes at HKEY_CURRENT_USER
RegNotifyChangeKeyValue HKEY_CURRENT_USER, True, REG_NOTIFY_ALL, ByVal 0&, False
MsgBox "Registry changed"
Unload Me
End Sub
---- public --------------------------------------------------------------
Option Explicit
Public Const ERROR_SUCCESS = 0&
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001
Public Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
Public Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const SYNCHRONIZE = &H100000
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
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)
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const REG_NOTIFY_CHANGE_NAME = &H1 ' Create or delete (child)
Public Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Public Const REG_NOTIFY_CHANGE_LAST_SET = &H4 ' time stamp
Public Const REG_NOTIFY_CHANGE_SECURITY = &H8
Public Const REG_NOTIFY_ALL = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
Public Const INFINITE = &HFFFF
Public Const WAIT_FAILED = -1&
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public 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 Long
'这里要注意一下,MSDN里面说lpEventAttributes这个参数Must be NULL,所以俺把声明改为:ByVal IsNull As Long
'Public Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Public Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal IsNull As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
---- public --------------------------------------------------------------
---- Form ----------------------------------------------------------
Option Explicit
'"Hide" Button
Private Sub Command1_Click()
Me.Hide
Call UninstallChangeMonitor
Me.Show
End Sub
'"Exit" Button
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Call UninstallChangeMonitor
End Sub
Private Sub UninstallChangeMonitor()
Dim lErrorCode As Long
Dim hMainKey As Long
Dim strSubKey As String
Dim hKey As Long
Dim lIsNull As Long
Dim hEvent As Long
If (lErrorCode <> ERROR_SUCCESS) Then
MsgBox "Error in RegOpenKeyEx."
Unload Me
End If
'Create an event.
lIsNull = 0
hEvent = CreateEvent(lIsNull, True, False, vbNullString)
If hEvent = Null Then
MsgBox "Error in CreateEvent."
Unload Me
End If
'Watch the registry key for a change of value.
lErrorCode = RegNotifyChangeKeyValue(hKey, _
True, _
REG_NOTIFY_ALL, _
hEvent, _
True)
If lErrorCode <> ERROR_SUCCESS Then
MsgBox "Error in RegNotifyChangeKeyValue."
End If
'Wait for an event to occur.
If WaitForSingleObject(hEvent, INFINITE) = WAIT_FAILED Then
MsgBox "Error in WaitForSingleObject."
GoTo goto_end
End If
Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
Text1.Text = Item.Tag
End Sub
Private Sub Command1_Click()
Dim hKey As Long
Dim sKey As String
Dim dwIndex As Long
Dim dwSubKeys As Long
Dim dwMaxSubKeyLen As Long
Dim ft As FILETIME
Dim success As Long
Dim sName As String
Dim cbName As Long
Dim itmx As ListItem
Dim rapp As REGISTRY_APPINFO
'obtain a handle to the uninstall key
sKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
hKey = OpenRegKey(HKEY_LOCAL_MACHINE, sKey)
'if valid
If hKey <> 0 Then
'query registry for the number of
'entries under that key
If RegQueryInfoKey(hKey, _
0&, _
0&, _
0, _
dwSubKeys, _
dwMaxSubKeyLen&, _
0&, _
0&, _
0&, _
0&, _
0&, _
ft) = ERROR_SUCCESS Then
'enumerate each item
For dwIndex = 0 To dwSubKeys - 1
This demo shows how to retrieve the listing of applications under the Uninstall key. In addition to the item names, the version (as stored in the registry with the file reference) as well as the actual uninstall string is returned. The item under the 'uninstallable' column is set True when an uninstall string is present; clicking such listview items will display the corresponding uninstall string in the textbox.
Private Const STANDARD_RIGHTS_READ As Long = &H20000
Private Const KEY_QUERY_VALUE As Long = &H1
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Private Const KEY_NOTIFY As Long = &H10
Private Const SYNCHRONIZE As Long = &H100000
Private Const KEY_READ As Long = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY) And _
(Not SYNCHRONIZE))
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2
Private Type REGISTRY_APPINFO
RegistryName As String
DisplayName As String
DisplayVersion As String
CanUninstall As Boolean
UninstallString As String
End Type
Private Type FILETIME 'ft
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private 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
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" _
Alias "RegEnumKeyExA" _
(ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, _
ByVal lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
lpftLastWriteTime As FILETIME) 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, _
lpData As Any, _
lpcbData As Long) As Long
Private Declare Function RegQueryInfoKey Lib "advapi32.dll" _
Alias "RegQueryInfoKeyA" _
(ByVal hKey As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
ByVal 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 RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
'Size each column based on the maximum of
'wither the ColumnHeader text width, or,
'if the items below it are wider, the
'widest list item in the column
lv.Visible = False
HANDLE hEvent;
HKEY hMainKey;
HKEY hKey;
LONG lErrorCode;
// Display the usage error message.
if (argc != 3)
{
printf("Usage: notify [HKLM/HKU/HKCU/HKCR/HCC] [subkey]\n");
return;
}
// Convert parameters to appropriate handles.
if (strcmp("HKLM", argv[1]) == 0)
hMainKey = HKEY_LOCAL_MACHINE;
else if (strcmp("HKU", argv[1]) == 0)
hMainKey = HKEY_USERS;
else if (strcmp("HKCU", argv[1]) == 0)
hMainKey = HKEY_CURRENT_USER;
else if (strcmp("HKCR", argv[1]) == 0)
hMainKey = HKEY_CLASSES_ROOT;
else if (strcmp("HCC", argv[1]) == 0)
hMainKey = HKEY_CURRENT_CONFIG;
else
{
printf("Usage: notify [HKLM/HKU/HKCU/HKCR/HCC] [subkey]\n");
return;
}
// Open a key.
lErrorCode = RegOpenKeyEx(hMainKey, argv[2], 0, KEY_NOTIFY, &hKey);
if (lErrorCode != ERROR_SUCCESS)
printf("Error in RegOpenKeyEx.\n");
// Create an event.
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hEvent == NULL)
printf("Error in CreateEvent.\n");
// Watch the registry key for a change of value.
lErrorCode = RegNotifyChangeKeyValue(hKey,
TRUE,
dwFilter,
hEvent,
TRUE);
if (lErrorCode != ERROR_SUCCESS)
printf("Error in RegNotifyChangeKeyValue.\n");
// Wait for an event to occur.
if (WaitForSingleObject(hEvent, INFINITE) == WAIT_FAILED)
printf("Error in WaitForSingleObject.\n");
// Close the key.
lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
printf("Error in RegCloseKey.\n");
// Close the handle.
if (!CloseHandle(hEvent))
printf("Error in CloseHandle.\n");
}
---- From MSDN End --------------------------------------------------------
Notifies the system of an event that an application has performed. An application should use this function if it performs an action that may affect the shell.