Getting notification on windows registry change for a key.

Discussion in 'C' started by rag84dec, Nov 13, 2009.

  1. rag84dec

    rag84dec New Member

    Joined:
    Jul 17, 2007
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    hi all,

    I am trying to write a code to notify if the registry key value has been changed in windows. I have appended the code below.
    Now, i want to get the information about the key that has been changed along with the "Change has occurred.". How to do??.
    can any one help please. Can i use WMI registry notification?.any sample code would help a lot.

    thanks in advance.


    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    
    //void main(int argc, char *argv[])
    void __cdecl _tmain(int argc, TCHAR *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;
    
       
       RegOpenKeyEx( HKEY_LOCAL_MACHINE,
            TEXT("System"),
            0,
            KEY_READ,
            &hKey);
       
    
       // Create an event.
       hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
       if (hEvent == NULL)
       {
          _tprintf(TEXT("Error in CreateEvent (%d).\n"), GetLastError());
          return;
       }
    
       // Watch the registry key for a change of value.
       lErrorCode = RegNotifyChangeKeyValue(hKey, 
                                            TRUE, 
                                            dwFilter, 
                                            hEvent, 
                                            TRUE);
       if (lErrorCode != ERROR_SUCCESS)
       {
          _tprintf(TEXT("Error in RegNotifyChangeKeyValue (%d).\n"), lErrorCode);
          return;
       }
    
       // Wait for an event to occur.
       _tprintf(TEXT("Waiting for a change in the specified key...\n"));
       if (WaitForSingleObject(hEvent, INFINITE) == WAIT_FAILED)
       {
          _tprintf(TEXT("Error in WaitForSingleObject (%d).\n"), GetLastError());
          return;
       }
       else
       {
    	   //Get chile events for the event key ... In this case select. Display the key name and values.
    
    	   _tprintf(TEXT("\nChange has occurred.\n"));
    	  // printf("the modified key is %s",hKey);
       }
    
       // Close the key.
       lErrorCode = RegCloseKey(hKey);
       if (lErrorCode != ERROR_SUCCESS)
       {
          _tprintf(TEXT("Error in RegCloseKey (%d).\n"), GetLastError());
          return;
       }
       
       // Close the handle.
       if (!CloseHandle(hEvent))
       {
          _tprintf(TEXT("Error in CloseHandle.\n"));
          return;
       }
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice