VC++实现注册表监控

来源:互联网 发布:鉴声师鉴声用什么软件 编辑:程序博客网 时间:2024/04/30 06:26


[cpp] view plaincopyprint?
  1. //监视HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run项 
  2.  
  3. #include <windows.h> 
  4.  
  5. //定义一个监视注册表启动项的函数 
  6. int reg() 
  7.    HANDLE hNotify; 
  8.    HKEY hKeyx; 
  9.    //DWORD dwRes; 
  10.  
  11.    hNotify = CreateEvent(NULL, //不使用SECURITY_ATTRIBUTES结构 
  12.          FALSE, //不自动重置 
  13.          TRUE,   //设置初始状态 
  14.          "RegistryNotify"//事件对象的名称 
  15.          ); 
  16.  
  17.    if (hNotify == 0) 
  18.    { 
  19.      MessageBox(NULL,"CreateEvent failed"," ",MB_OK); 
  20.      ExitProcess(0); 
  21.    } 
  22.  
  23.    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,//根键 
  24.          "Software\\Microsoft\\Windows\\CurrentVersion\\Run",//子键 
  25.          0, //reserved 
  26.          KEY_NOTIFY, //监视用 
  27.          &hKeyx //保存句柄 
  28.          ) != ERROR_SUCCESS) 
  29.    { 
  30.      CloseHandle(hNotify); 
  31.      MessageBox(NULL,"RegOpenKeyEx failed"," ",MB_OK); 
  32.      ExitProcess(0); 
  33.    } 
  34.  
  35.    if (RegNotifyChangeKeyValue(hKeyx,//监视子键句柄 
  36.          TRUE, //监视此项的子键 
  37.          REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, //监视增加或删除了子键,监视键值发生是否改变 
  38.          hNotify, //接受注册表变化事件的事件对象句柄 
  39.          TRUE //注册表变化前报告 
  40.          ) != ERROR_SUCCESS) 
  41.    { 
  42.      CloseHandle(hNotify); 
  43.      RegCloseKey(hKeyx); 
  44.      MessageBox(NULL,"RegNotifyChangeKeyValue failed"," ", MB_OK); 
  45.      ExitProcess(0); 
  46.    } 
  47.  
  48.    if (WaitForSingleObject(hNotify, INFINITE) != WAIT_FAILED) 
  49.    { 
  50.      MessageBox(NULL,"注册表有改动"," ",MB_OK); 
  51.    } 
  52.  
  53.    CloseHandle(hNotify); 
  54.    RegCloseKey(hKeyx); 
  55.  
  56.    return 0; 
  57.  
  58.  
  59. void main() 
  60. // DWORD ID; 
  61.  
  62. // CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)reg, NULL, 0, &ID);   //创建线程 
  63.  
  64. // printf("ok\n"); 
  65.  
  66.     reg(); 
  67.  
  68. }

原文地址:http://blog.csdn.net/yincheng01/article/details/8107304