使用RegNotifyChangeKeyValue函数获取注册表通知时出现问题. [英] Problem getting Registry Notifications using RegNotifyChangeKeyValue function..

查看:331
本文介绍了使用RegNotifyChangeKeyValue函数获取注册表通知时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当注册表值更改时,我正在尝试获取通知.但是有些通知是不会来的.下面是示例代码...需要帮助才能知道什么地方出了错,这让我无法获得通知.

我在具有管理员权限的Win7机器上使用VS2010进行了尝试...

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) 
{ 
......
......
}

推荐答案

请尝试通过以下方式调整您的权限:
SE_CHANGE_NOTIFY_NAME或名称"SeChangeNotifyPrivilege"
Please try to adjust your privileges with:
SE_CHANGE_NOTIFY_NAME or the Name "SeChangeNotifyPrivilege"


这篇关于使用RegNotifyChangeKeyValue函数获取注册表通知时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆