Home > Windows API > Watching out for registry key changes using RegNotifyChangeKeyValue!

Watching out for registry key changes using RegNotifyChangeKeyValue!

We can watch out for changes on a key in the registry using the function RegNotifyChangeKeyValue. Here is a demo on how to do this. 🙂

// Helper function
void RegWatchOut( HANDLE& hEvent )
{
   const DWORD dwEventFilter = REG_NOTIFY_CHANGE_NAME |
                               REG_NOTIFY_CHANGE_ATTRIBUTES |
                               REG_NOTIFY_CHANGE_LAST_SET |
                               REG_NOTIFY_CHANGE_SECURITY;   

   RegNotifyChangeKeyValue( HKEY_LOCAL_MACHINE,
                            TRUE,
                            dwEventFilter,
                            hEvent,
                            TRUE );      
}   

int main( int argc, char* argv[] )
{
   HANDLE hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );

   // Call for notification
   RegWatchOut( hEvent );   

   // Keep looping for ever
   for( int Index = 1; WaitForSingleObject( hEvent, INFINITE ) != WAIT_FAILED; ++Index )
   {
        printf( "Key changed %d time(s)\\n", Index );   

        // Keep watching, we have to call again for further notifications
        RegWatchOut( hEvent );
   }

   CloseHandle( hEvent );
   return 0; 
}// End main

With this application running start any application ( For eg: Internet explorer ) to have some fun. 😀

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: