看看这份资料Alot of times, programming internet applications you need to know wether the computer is connected to the internet? In some occasions you can try to use RasEnumConnections, in most cases on a typical computer opened Ras Connection mean that the user is connected to the internet. However, this idea won't work in case the user is connected to internet not via Ras device or is connected to some kind of local network. We try to use common sence here. Since internet is something abstract, lets decide that user which is connected to internet, is the one who can connect to www.microsoft.com. First I wanted to use ping, but found a lot of troubles with it. Therefore, I have decided to use sockets "directly". I didn't want to write some fance code here, rather I have wrote a simple function you can cut & paste into your application. Another problem adressed in the function that in some computer a try to establish internet connection will bring dialup dialog. The function solve it by temporary disabling the feature in the registry and enabling it back afterwards. Pay attention to the comments. BOOL YourClassHere::IsInternetConnected (void) { int nCheck = AfxSocketInit(); CSocket m_Server; HKEY hKey; DWORD dwDial, dwDialType = REG_DWORD, dwDialSize = 4; DWORD dwNew = 0; BOOL bResult = true;
if ( RegOpenKeyEx ( HKEY_CURRENT_USER, Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS) ; // We cannot find the key. Handle this situation or just continue
if ( RegQueryValueEx( hKey, "EnableAutodial", NULL, &dwDialType, (BYTE *) &dwDial, &dwDialSize ) != ERROR_SUCCESS ) ; // We cannot find the value. Handle it. if ( dwDial ) { // We need to change the value, in order to make // a dialup window not to show up.
if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL, dwDialType, (BYTE *) &dwNew, dwDialSize )) != ERROR_SUCCESS) ; // Failed? We shouldn't get here. You decide how to handle it }
if ( !m_Server.Create() ) { // m_sError = _T( "Unable to create the socket." ); bResult = false; } // You can use www.microsoft.com in order to check whether DNS is available // or numeric IP otherwise else if ( !m_Server.Connect( "www.microsoft.com", 80 ) ) { // 207.46.130.150 //m_sError = _T( "Unable to connect to server" ); m_Server.Close(); bResult = false; }
if ( dwDial ) { if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL, dwDialType, (BYTE *) &dwDial, dwDialSize )) != ERROR_SUCCESS) ; // Failed? We shouldn't get it. You decide how to handle this. }
RegCloseKey( hKey ); return ( bResult ); }
以下代码保证可用(建在模块中),因为我正在用:) Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long '----------------以下用于监测是否连网---------------- Public Const RAS95_MaxEntryName = 256 Public Const RAS95_MaxDeviceType = 16 Public Const RAS95_MaxDeviceName = 32Public Type RASCONN95 dwSize As Long hRasCon As Long szEntryName(RAS95_MaxEntryName) As Byte szDeviceType(RAS95_MaxDeviceType) As Byte szDeviceName(RAS95_MaxDeviceName) As Byte End TypePublic Type RASCONNSTATUS95 dwSize As Long RasConnState As Long dwError As Long szDeviceType(RAS95_MaxDeviceType) As Byte szDeviceName(RAS95_MaxDeviceName) As Byte End TypePublic Function IsConnected() As Boolean Dim TRasCon(255) As RASCONN95 Dim lg As Long Dim lpcon As Long Dim RetVal As Long Dim Tstatus As RASCONNSTATUS95 TRasCon(0).dwSize = 412 lg = 256 * TRasCon(0).dwSize
RetVal = RasEnumConnections(TRasCon(0), lg, lpcon) If RetVal <> 0 Then MsgBox "软件运行出错!", vbOKOnly + vbInformation, "提示" Exit Function End If Tstatus.dwSize = 160 RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus) If Tstatus.RasConnState = &H2000 Then IsConnected = True '连网成功 Else IsConnected = False '连网失败 End If End Function '---------------------------------------------------
BOOL YourClassHere::IsInternetConnected (void)
{
int nCheck = AfxSocketInit();
CSocket m_Server;
HKEY hKey;
DWORD dwDial, dwDialType = REG_DWORD, dwDialSize = 4;
DWORD dwNew = 0;
BOOL bResult = true;
if ( RegOpenKeyEx ( HKEY_CURRENT_USER,
Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
; // We cannot find the key. Handle this situation or just continue
if ( RegQueryValueEx( hKey, "EnableAutodial", NULL, &dwDialType,
(BYTE *) &dwDial, &dwDialSize ) != ERROR_SUCCESS )
; // We cannot find the value. Handle it. if ( dwDial ) { // We need to change the value, in order to make
// a dialup window not to show up.
if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
dwDialType, (BYTE *) &dwNew, dwDialSize )) != ERROR_SUCCESS)
; // Failed? We shouldn't get here. You decide how to handle it
}
if ( !m_Server.Create() ) {
// m_sError = _T( "Unable to create the socket." );
bResult = false;
} // You can use www.microsoft.com in order to check whether DNS is available
// or numeric IP otherwise
else if ( !m_Server.Connect( "www.microsoft.com", 80 ) ) { // 207.46.130.150
//m_sError = _T( "Unable to connect to server" );
m_Server.Close();
bResult = false;
}
if ( dwDial ) {
if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
dwDialType, (BYTE *) &dwDial, dwDialSize )) != ERROR_SUCCESS)
; // Failed? We shouldn't get it. You decide how to handle this.
}
RegCloseKey( hKey );
return ( bResult );
}
Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
'----------------以下用于监测是否连网----------------
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32Public Type RASCONN95
dwSize As Long
hRasCon As Long
szEntryName(RAS95_MaxEntryName) As Byte
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End TypePublic Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End TypePublic Function IsConnected() As Boolean
Dim TRasCon(255) As RASCONN95
Dim lg As Long
Dim lpcon As Long
Dim RetVal As Long
Dim Tstatus As RASCONNSTATUS95 TRasCon(0).dwSize = 412
lg = 256 * TRasCon(0).dwSize
RetVal = RasEnumConnections(TRasCon(0), lg, lpcon)
If RetVal <> 0 Then
MsgBox "软件运行出错!", vbOKOnly + vbInformation, "提示"
Exit Function
End If Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
If Tstatus.RasConnState = &H2000 Then
IsConnected = True '连网成功
Else
IsConnected = False '连网失败
End If
End Function
'---------------------------------------------------