KEY_NOTIFY 有什么用

搜索 KEY_NOTIFY的结果是:许可提供更该通知,
msdn上说,但并没有详细解释

Required to request change notifications for a registry key or for subkeys of a registry key.

其实这个值与

Required to request


 change notifications for a registry key or for subkeys of a registry key.


RegNotifyChangeKeyValue 函数有关

代码

Required to request


 change notifications for a registry key or for subkeys of a registry key.


#include <stdio.h>
#include <windows.h>

void main(int argc, char *argv[])
{
   DWORD  dwFilter = REG_NOTIFY_CHANGE_NAME |
                     REG_NOTIFY_CHANGE_ATTRIBUTES |
                     REG_NOTIFY_CHANGE_LAST_SET |
                     REG_NOTIFY_CHANGE_SECURITY; 

   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");
      return;
   }

   // Create an event.
   hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (hEvent == NULL)
   {
      printf("Error in CreateEvent.\n");
      return;
   }

   // 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");
      return;
   }

   // Wait for an event to occur.
   if (WaitForSingleObject(hEvent, INFINITE) == WAIT_FAILED)
   {
      printf("Error in WaitForSingleObject.\n");
      return;
   }

   // Close the key.
   lErrorCode = RegCloseKey(hKey);
   if (lErrorCode != ERROR_SUCCESS)
   {
      printf("Error in RegCloseKey.\n");
      return;
   }

   // Close the handle.
   if (!RegCloseKey(hEvent))
   {
      printf("Error in RegCloseKey.\n");
      return;
   }
}






Required to request


 change notifications for a registry key or for subkeys of a registry key.

相关文章
相关标签/搜索