|
- #include <shellapi.h>
- #include <Shlobj.h>
- int main() {
- HKEY hKey;
- LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\test", 0, KEY_NOTIFY, &hKey);
- if (result == ERROR_SUCCESS) {
- //创建事件
- HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (hEvent != NULL) {
- while (true) {
- //注册通知
- result = RegNotifyChangeKeyValue(hKey, TRUE, REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET, hEvent, TRUE);
- if (result != ERROR_SUCCESS) {
- printf("RegNotifyChangeKeyValue失败\n");
- break;
- }
- //等待通知
- DWORD waitResult = WaitForSingleObject(hEvent, INFINITE);
- if (waitResult == WAIT_OBJECT_0) {
- printf("发生变化!\n");
- }
-
- }
- CloseHandle(hEvent);
- RegCloseKey(hKey);
- }
- }
- return 0;
- }
复制代码
|
|