Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Getting notification on windows registry change for a key. (http://www.go4expert.com/forums/getting-notification-windows-registry-t20080/)

rag84dec 13Nov2009 15:27

Getting notification on windows registry change for a key.
 
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;
  }
}



All times are GMT +5.5. The time now is 14:50.