VC++レジストリ監視を実現

1631 ワード

//  HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 

#include <windows.h>

//               
int reg()
{
   HANDLE hNotify;
   HKEY hKeyx;
   //DWORD dwRes;

   hNotify = CreateEvent(NULL, //   SECURITY_ATTRIBUTES  
         FALSE, //     
         TRUE,   //      
         "RegistryNotify" //       
         );

   if (hNotify == 0)
   {
     MessageBox(NULL,"CreateEvent failed"," ",MB_OK);
     ExitProcess(0);
   }

   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, //  
         "Software\\Microsoft\\Windows\\CurrentVersion\\Run", //  
         0, //reserved
         KEY_NOTIFY, //   
         &hKeyx //    
         ) != ERROR_SUCCESS)
   {
     CloseHandle(hNotify);
     MessageBox(NULL,"RegOpenKeyEx failed"," ",MB_OK);
     ExitProcess(0);
   }

   if (RegNotifyChangeKeyValue(hKeyx, //      
         TRUE, //       
         REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, //          ,          
         hNotify, //                
         TRUE //        
         ) != ERROR_SUCCESS)
   {
     CloseHandle(hNotify);
     RegCloseKey(hKeyx);
     MessageBox(NULL,"RegNotifyChangeKeyValue failed"," ", MB_OK);
     ExitProcess(0);
   }

   if (WaitForSingleObject(hNotify, INFINITE) != WAIT_FAILED)
   {
     MessageBox(NULL,"      "," ",MB_OK);
   }

   CloseHandle(hNotify);
   RegCloseKey(hKeyx);

   return 0;

}

void main()
{
 // DWORD ID;

 // CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)reg, NULL, 0, &ID);   //    

 // printf("ok
"); reg(); }