无忧资源站

 找回密码
 加入我们
查看: 254|回复: 0

[C/C++] C++应用层监视注册表变化

[复制链接]

12

主题

15

帖子

101

积分

学徒

Rank: 2

积分
101
发表于 2024-3-29 22:18:43 | 显示全部楼层 |阅读模式
  1. #include <shellapi.h>
  2. #include <Shlobj.h>
  3. int main() {

  4.   HKEY hKey;
  5.     LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\test", 0, KEY_NOTIFY, &hKey);
  6.     if (result == ERROR_SUCCESS) {
  7.         //创建事件
  8.         HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  9.         if (hEvent != NULL) {
  10.             while (true) {
  11.                 //注册通知
  12.                 result = RegNotifyChangeKeyValue(hKey, TRUE, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, hEvent, TRUE);
  13.                 if (result != ERROR_SUCCESS) {
  14.                     printf("RegNotifyChangeKeyValue失败\n");
  15.                     break;
  16.                 }
  17.                 //等待通知
  18.                 DWORD waitResult = WaitForSingleObject(hEvent, INFINITE);
  19.                 if (waitResult == WAIT_OBJECT_0) {
  20.                     printf("发生变化!\n");
  21.                 }
  22.          
  23.             }
  24.             CloseHandle(hEvent);
  25.             RegCloseKey(hKey);
  26.         }
  27.     }

  28.     return 0;
  29. }
复制代码


QQ|SiteMap|Map|小黑屋|无忧资源站

GMT+8, 2024-5-7 09:03 , Processed in 0.054811 second(s), 19 queries .

无忧资源站

Copyright © 2022-2032 www.wyzyz.net

快速回复 返回顶部 返回列表