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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章