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...
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
DWORD WINAPI RegNotifyProc( LPVOID lpParam );
void ErrorHandler(LPTSTR lpszFunction);
typedef struct MyData {
int val1;
int val2;
} MYDATA, *PMYDATA;
int _tmain()
{
PMYDATA pDataArray;
DWORD dwThreadIdArray;
HANDLE hThreadArray;
pDataArray = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(MYDATA));
if( pDataArray == NULL )
{
ExitProcess(2);
}
pDataArray->val1 = 1;
pDataArray->val2 = 100;
hThreadArray = CreateThread(
NULL,
0,
RegNotifyProc,
pDataArray,
0,
&dwThreadIdArray);
if (hThreadArray == NULL)
{
ErrorHandler(TEXT("CreateThread"));
ExitProcess(3);
}
WaitForSingleObject(hThreadArray, INFINITE);
CloseHandle(hThreadArray);
if(pDataArray != NULL)
{
HeapFree(GetProcessHeap(), 0, pDataArray);
pDataArray = NULL;
}
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;
lErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"software\\DevX", 0,KEY_NOTIFY | KEY_READ, &hKey);
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
lErrorCode = RegNotifyChangeKeyValue(hKey, TRUE, dwFilter, hEvent,
FALSE);
WaitForSingleObject(hEvent, INFINITE);
lErrorCode = RegQueryValueEx(hKey,L"User",0,&dwType,(unsigned char*)lpszUser,&dwSize);
printf("Modified... : %s\n",lpszUser);
lErrorCode = RegCloseKey(hKey);
CloseHandle(hEvent);
Sleep(1);
}
return 1;
}
void ErrorHandler(LPTSTR lpszFunction)
{
......
......
}