Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get notifications when a registry value changes. but some how the notifications are not coming. Below is the sample code ... Need help to know what is wrong which is not letting me get the notifications.

I tried it with VS2010 on a Win7 machine with admin rights...

// NotifyReg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>


DWORD WINAPI RegNotifyProc( LPVOID lpParam );
void ErrorHandler(LPTSTR lpszFunction);

// Sample custom data structure for threads to use.
// This is passed by void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
typedef struct MyData {
    int val1;
    int val2;
} MYDATA, *PMYDATA;



int _tmain()
{
    PMYDATA pDataArray;
    DWORD   dwThreadIdArray;
    HANDLE  hThreadArray; 

    // Create MAX_THREADS worker threads.

    
        // Allocate memory for thread data.

        pDataArray = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                sizeof(MYDATA));

        if( pDataArray == NULL )
        {
           // If the array allocation fails, the system is out of memory
           // so there is no point in trying to print an error message.
           // Just terminate execution.
            ExitProcess(2);
        }

        // Generate unique data for each thread to work with.

        pDataArray-&gt;val1 = 1;
        pDataArray-&gt;val2 = 100;

        // Create the thread to begin execution on its own.

        hThreadArray = CreateThread( 
            NULL,                   // default security attributes
            0,                      // use default stack size  
            RegNotifyProc,       // thread function name
            pDataArray,          // argument to thread function 
            0,                      // use default creation flags 
            &dwThreadIdArray);   // returns the thread identifier 


        // Check the return value for success.
        // If CreateThread fails, terminate execution. 
        // This will automatically clean up threads and memory. 

        if (hThreadArray == NULL) 
        {
           ErrorHandler(TEXT("CreateThread"));
           ExitProcess(3);
        }
    // End of main thread creation loop.

    // Wait until all threads have terminated.

    WaitForSingleObject(hThreadArray, INFINITE);

    // Close all thread handles and free memory allocations.
 
    CloseHandle(hThreadArray);
    if(pDataArray != NULL)
    {
        HeapFree(GetProcessHeap(), 0, pDataArray);
        pDataArray = NULL;    // Ensure address is not reused.
    }
    return 0;
}


DWORD WINAPI RegNotifyProc(LPVOID x) 
{

	DWORD  dwFilter = REG_NOTIFY_CHANGE_NAME |
                     REG_NOTIFY_CHANGE_ATTRIBUTES |
                     REG_NOTIFY_CHANGE_LAST_SET |
                     REG_NOTIFY_CHANGE_SECURITY,dwType, dwSize ; 

	char lpszUser[81];
	HANDLE hEvent;
	HKEY   hKey;
	LONG   lErrorCode;

	while(1) 	
	{
		memset(lpszUser,0,81);
		dwSize = 81;
		dwType = REG_SZ;

		// Open a key. Change second parameter to fit your needs.
		lErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"software\\DevX", 0,KEY_NOTIFY | KEY_READ, &hKey);

		// Create an event.
		hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

		// Watch the registry key for a change of value.
		lErrorCode = RegNotifyChangeKeyValue(hKey, TRUE, dwFilter, hEvent,
			FALSE);

		// Wait for an event to occur.
		WaitForSingleObject(hEvent, INFINITE);

		lErrorCode = RegQueryValueEx(hKey,L"User",0,&dwType,(unsigned char*)lpszUser,&dwSize);
		//Add code for reading from the registry key
		printf("Modified... : %s\n",lpszUser);

		// Close the key.
		lErrorCode = RegCloseKey(hKey);

		// Close the handle.
		CloseHandle(hEvent);
		Sleep(1);
	}
	return 1; 
}

void ErrorHandler(LPTSTR lpszFunction) 
{ 
......
......
}
Posted
Updated 19-Jan-12 2:29am
v4
Comments
Emilio Garavaglia 19-Jan-12 8:29am    
HTML re-encoded

1 solution

Please try to adjust your privileges with:
SE_CHANGE_NOTIFY_NAME or the Name "SeChangeNotifyPrivilege"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900