<!-- generator=" CodePongo (Base on Kukkaisvoima version 15b3TA)" -->
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<channel><title>CodePongo: webbrowser</title><link>http://codepongo.com/blog</link><description></description><pubDate>Thu, 12 Jun 2014 17:30:06 +0200</pubDate><lastBuildDate>Thu, 12 Jun 2014 17:30:06 +0200</lastBuildDate><generator>http://codepongo.com/blog</generator><language>zh-cn</language><item><title>network awareness in windows获取windows网络状态
</title><link>http://codepongo.com/blog/hYACn</link><comments>http://codepongo.com/blog/hYACn#comments</comments><pubDate>Thu, 12 Jun 2014 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>windows</category><category>webbrowser</category><guid isPermaLink="false">http://codepongo.com/blog/hYACn/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
获取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
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


]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/hYACn/feed/</wfw:commentRss></item></channel></rss>