您的位置:首页 > 其它

InternetCheckConnection 通过尝试连接特定的网址,可以用来判断目前机器是否接通外网

2014-12-24 14:46 465 查看
InternetCheckConnection 通过尝试连接特定的网址,可以用来判断目前机器是否接通外网。例如:

BOOL bConnected = InternetCheckConnection(_T("http://www.google.com"), FLAG_ICC_FORCE_CONNECTION, 0);

使用时第二个参数一定要设置为FLAG_ICC_FORCE_CONNECTION,这样才会尝试去建立连接。当目标地址不可用时,这个函数返回会比较慢,注意不要在UI线程中使用。

这个函数会解析出URL中的host地址,然后建立socket连接去ping这个地址。


InternetCheckConnection

Allows an application to check if a connection to the Internet can be established.
<strong>BOOL InternetCheckConnection(
LPCTSTR</strong> <em><a target=_blank class="synParam" href="http://blog.csdn.net/harbinzju/article/details/6956814" style="color: rgb(202, 0, 0); text-decoration: none;">lpszUrl</a></em><strong>,
DWORD</strong> <em><a target=_blank class="synParam" href="http://blog.csdn.net/harbinzju/article/details/6956814" style="color: rgb(202, 0, 0); text-decoration: none;">dwFlags</a></em><strong>,
DWORD</strong> <em><a target=_blank class="synParam" href="http://blog.csdn.net/harbinzju/article/details/6956814" style="color: rgb(202, 0, 0); text-decoration: none;">dwReserved</a></em><strong>
);</strong>


Parameters

lpszUrl[in] Pointer to a null-terminated string that specifies the URL to use to check the connection. This value can be NULL.dwFlags[in] Options. FLAG_ICC_FORCE_CONNECTION is the only flag that is currently available. If this flag is set, it forces a connection. A sockets connection is attempted in the following order:
If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host.
If lpszUrl is NULL and there is an entry in the internal server database for the nearest server, the host value is extracted from the entry and used to ping that server.
dwReserved[in] Reserved. Must be zero.


Return Values

Returns TRUE if
a connection is made successfully, or FALSE otherwise. Use GetLastError to
retrieve the error code. ERROR_NOT_CONNECTED is returned by GetLastError if a
connection cannot be made or if the sockets database is unconditionally offline.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐