win32 - 监控DNS是否发生改变

两种方法:

第一种是使用WMI进行后台轮询

第二种是查询注册表对应的DNS键值

Here: HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces[\interface-name]

使用RegNotifyChangeKeyValue进行监控

顺便一提:

想要监控主机的IP地址,子网掩码或默认网关是否更改,可以使用NotifyRouteChange或者NotifyIpInterfaceChange 来发送通知。

一些代码:

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <windows.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

void main()
{
  OVERLAPPED overlap;
  DWORD ret;
    
  HANDLE hand = NULL;
  overlap.hEvent = WSACreateEvent();

  ret = NotifyRouteChange(&hand, &overlap);

  if (ret != NO_ERROR)
  {
    if (WSAGetLastError() != WSA_IO_PENDING)
    {
      printf("NotifyRouteChange error...%d\n", WSAGetLastError());            
      return;
    }
  }

  if ( WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0 )
    printf("Routing table changed..\n");
}

 

posted @ 2020-10-12 09:30  strive-sun  阅读(349)  评论(0编辑  收藏  举报