两种方法:
第一种是使用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"); }
原文链接: https://www.cnblogs.com/strive-sun/p/13800978.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/404819
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!