获取windows网络状态有三种方式:
InternetGetConnectedState(wininet.dll)直接获取
GetAdaptersAddresses(iphlpapi.dll)枚举网络适配器
Network Awareness
NLA in Windows XP
NLM in Vista or windows 7
源代码 source code
netaware - https://raw.githubusercontent.com/codepongo/utocode/master/windows/netaware.cpp
- with iphlp - webkit的GetAdaptersAddresses方式的实现 webkit implementation
- with nlm - NLM的同步实现 NLM synchronous implementation
- with nlm event - NLM的异步实现 NLM asynchronous implementation
- with wininet - InternetGetConnectedState的实现 InternetGetConnectedState implementation
- with winsock - NLA的同步实现 NLA synchronous implementation
- wait for change with winsock - NLA的异步实现 NLA asynchronous implementation
- GetAdaptersAddresses方式的更准确实现可参考firefox的源码
- 如果确定不了网络是否连接(如API失败等情况),则返回已连接状态
- 通过获取AdapaterAddress AdapaterInfo和IPAddressTable进行综合判断
- 通过NotifyAddrChange(http://msdn.microsoft.com/en-us/library/windows/desktop/aa366329.aspx)异步通知 伪代码如下:
def CheckAdapterAddress():
addresses = GetAdaptersAddresses()
for a in addresses:
if a.OperStatus == IfOperStatusUp
and a.IfType != IF_TYPE_SOFTWARE_LOOPBACK
and a.FirstUnicastAddress->Address.lpSockaddr != '192.168.0.1':
return True;
return False
def CheckAdaptersInfo():
adapters = GetAdaptersInfo()
for a in adapters:
if a.DhcpEnabled:
if a.DhcpServer.IpAddress == '255.255.255.255':
return True;
else:
for ip in a.IpAddressList:
if ip == '0.0.0.0':
return True;
return False;
def CheckIPAddrTable():
tables = GetIpAddrTable()
for t in tables:
if t.dwAddr != 0 && t.dwAddr != 0x0100007F:
return True
return False
reference 参考
- chromium source code
- Network Awareness
- NLA in Windows XP
- NLM in windowx XP later
- GetAdaptersAddresses in firefox
- GetAdaptersAddresses in webkit
- How to automatic get Network change state
- source code in How to automatic get Network change state
- source code in How to automatic get Network change state
- GetAdaptersAddresses
标签:
windows,
webbrowser
日期: 2014-06-12 17:30:06, 11 years and 147 days ago
