http://www.pcausa.com/resources/InetActive.txt From: Allen Weng <[email protected]> Subject: RE: Detecting if internet connection is active at this moment. Date: Friday, May 04, 2001 2:26 AMActually, there is no single function for determining if a machine is connected to the Internet, and it is impossible to reliably determine what is happening without side effects - such as automatic network connections taking place. What you can do is reliably detect when there definitely isn't an Internet Link: in the absence of any dial up or LAN connection the system is definitely off line. Some techniques include:1. IsNetworkAlive() If you are targeting system with IE5 or later, this is the best API call yet it even listens for traffic on a LAN. There is a secondary function IsDestinationReachable() which tries to resolve the hostname and ping it. This does not work through firewalls, and overestimates speed as the max the LAN card can support, rather than the actual point to point bandwidth.2. RasEnumConnections() A reliable technique for modems and direct dial up networking, but not for situations where Internet access is via a LAN. You should dynamically load "RasEnumConnectionA" from "RASAPI32.DLL", as LAN installations of Windows may not include the library.3. InternetGetConnectedState() This Wininet /IE4 function call can distinguish between modem and LAN, but can't handle complex LAN+autodial router situations. It is "offline state aware". Important: handling of the offline flage changed for IE5 -it returns TRUE for connected' even when off line, but signals the flags in the LPDWORD parameter.4. InternetCheckConnection() A Winnet/IE4 function call. This is meant to determine if a URL is reachable- in practice it is pretty unreliable and best voided.5. NT SP4, NT5: The IP helper API can tell you which network interface to use to connect to a supplied IP address, and what the bandwidth and current status of that link is6. Using the Offline flag which is part of IE4 to allow users to manually control the online/offline state of applications. This flag is stored in the registry and can be manipulated via some funcions callsThese calls mostly determine the presence or absence of network connections -not Internet access, so can't handle a home network sharing a dial up connection, or two laptops connected directly to each other. The global offline state flag of IE4 (and hence win98, NT5) and the call to test it - InternetGetConnectedState()- look the best long term options, but will take time to become universal. The IP Helper APIs even let you find out how much traffic is going over a link, but only detect the 'loopback' interface on Windows 98, so is not a lot of use. Wouldn't a 'GetSpeedToHost() function call be great? Finally, whatever technique you use, when it's time to talk to a remote site, always add timeouts or a cancel button. Even a quick functions like gethostbyname() can lock up an app if something in the network chain is broken. Allen Weng Microsoft Developer Support
原代码下载:download函数原型如下:BOOL WINAPI PL_DetectInternetConnect(CString strHTTP);
参数含义:
strHTTP -- 指定一个可靠的网址,如:"http://www.163.com"函数实现如下:
BOOL WINAPI PL_DetectInternetConnect(CString strHTTP)
{
BOOL bRet = FALSE;
HINTERNET hSession = NULL;
hSession = ::InternetOpen(_T("peeper"), PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, 0);
if(hSession)
{
HINTERNET hHTTPSession = ::InternetOpenUrl(hSession,
strHTTP, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if(hHTTPSession)
{
bRet = TRUE;
::InternetCloseHandle(hHTTPSession);
}
::InternetCloseHandle(hSession);
}
return bRet;
}
http://www.pcausa.com/resources/InetActive.txt
From: Allen Weng <[email protected]>
Subject: RE: Detecting if internet connection is active at this moment.
Date: Friday, May 04, 2001 2:26 AMActually, there is no single function for determining if a machine is
connected to the Internet, and it is impossible to reliably determine what
is happening without side effects - such as automatic network connections
taking place. What you can do is reliably detect when there definitely
isn't an Internet Link: in the absence of any dial up or LAN connection the
system is definitely off line. Some techniques include:1. IsNetworkAlive()
If you are targeting system with IE5 or later, this is the best API call
yet it even listens for traffic on a LAN. There is a secondary function
IsDestinationReachable() which tries to resolve the hostname and ping it.
This does not work through firewalls, and overestimates speed as the max
the LAN card can support, rather than the actual point to point bandwidth.2. RasEnumConnections()
A reliable technique for modems and direct dial up networking, but not for
situations where Internet access is via a LAN. You should dynamically load
"RasEnumConnectionA" from "RASAPI32.DLL", as LAN installations of Windows
may not include the library.3. InternetGetConnectedState()
This Wininet /IE4 function call can distinguish between modem and LAN, but
can't handle complex LAN+autodial router situations. It is "offline state
aware". Important: handling of the offline flage changed for IE5 -it
returns TRUE for connected' even when off line, but signals the flags in
the LPDWORD parameter.4. InternetCheckConnection()
A Winnet/IE4 function call. This is meant to determine if a URL is
reachable- in practice it is pretty unreliable and best voided.5. NT SP4, NT5: The IP helper API can tell you which network interface to
use to connect to a supplied IP address, and what the bandwidth and current
status of that link is6. Using the Offline flag which is part of IE4 to allow users to manually
control the online/offline state of applications. This flag is stored in
the registry and can be manipulated via some funcions callsThese calls mostly determine the presence or absence of network connections
-not Internet access, so can't handle a home network sharing a dial up
connection, or two laptops connected directly to each other. The global offline state flag of IE4 (and hence win98, NT5) and the call to
test it - InternetGetConnectedState()- look the best long term options, but
will take time to become universal. The IP Helper APIs even let you find
out how much traffic is going over a link, but only detect the 'loopback'
interface on Windows 98, so is not a lot of use. Wouldn't a
'GetSpeedToHost() function call be great? Finally, whatever technique you use, when it's time to talk to a remote
site, always add timeouts or a cancel button. Even a quick functions like
gethostbyname() can lock up an app if something in the network chain is
broken. Allen Weng
Microsoft Developer Support
bool InternetGetConnectedState(OUT LPDWORD lpdwFlags,IN DWORD dwReserved);取得本机连接状态
其中返回值
lpdwFlags为本机当前状态
0x1 为通过调制解调器连上internet
0x2 为通过局域网连上internet
0x4 为通过代理服务器连上internet
0x8 调制解调器忙线中
0x10 是否安装ras
0x20 是否脱机
InternetGetConnectedState(&dwFlag,0);
if(dwFlag&0x01)
{
AfxMessageBox("目前通过拨号连接Internet")
}
if(dwFlag&0x02)
{
AfxMessageBox("目前通过局域网连接");
}