usernckv.c

来自「C#一个实例源代码」· C语言 代码 · 共 72 行

C
72
字号
#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");

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?