在vb6中检测Internet是否连接

来源:互联网 发布:金融信息基础数据库 编辑:程序博客网 时间:2024/05/22 03:09

'有关的API声明和定义
Private Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As LongAs Long
Private Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
'常数和变量的设定
Const RAS95_MaxEntryName = 256
Const RAS95_MaxDeviceType = 16
Const RAS95_MaxDeviceName = 32

Private Type RASCONN95
  dwSize 
As Long
  hRasCon 
As Long
  szEntryName(RAS95_MaxEntryName) 
As Byte
  szDeviceType(RAS95_MaxDeviceType) 
As Byte
  szDeviceName(RAS95_MaxDeviceName) 
As Byte
End Type

Private Type RASCONNSTATUS95
  dwSize 
As Long
  RasConnState 
As Long
  dwError 
As Long
  szDeviceType(RAS95_MaxDeviceType) 
As Byte
  szDeviceName(RAS95_MaxDeviceName) 
As Byte
End Type

'函数IsConnected返回连通的状态,为True则表示已连通

Public Function IsConnected() As Boolean
  
Dim TRasCon(255As 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 "错误"
    
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


Private Sub Form_Load()
  
If IsConnected Then
    
MsgBox ("您已经连通了Internet!")
  
End If
  
If Not IsConnected Then
    
MsgBox ("您还没有连通 Internet!")
  
End If
End Sub
 
原创粉丝点击