华中师范大学资助中心:VB/.NET 如何检测本机是否已经连接上网络?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 07:54:23
VB怎么知道ADSL是否已经连接上了?
俺只有初学水平 请说清楚点哦~``

新建工程,加入一模块,代码如下:
Option Explicit
Public isolerr As Boolean
Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Public 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 = 32
'
Public 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
'
Public Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Public 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 "出现错误!", vbCritical + vbDefaultButton1 + vbOKOnly, "错误!"
Form1.wangluozhuangkuang.Visible = False
isolerr = True
Exit Function
End If
'
isolerr = False
Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
If Tstatus.RasConnState = &H2000 Then
IsConnected = True
Else
IsConnected = False
End If

End Function

在窗体中添加一记时器(Timer),代码如下:
Private Sub Timer1_Timer()
checkonline
End Sub
然后在通用声明中添加如下代码
Sub checkonline()
If isolerr = False Then
If IsConnected = True Then
wangluozhuangkuang.Caption = "已经连接网络"
Else
wangluozhuangkuang.Caption = "尚未连接网络"
End If
End If
End Sub
最后在窗体上添加一Label,名字为wangluozhuangkuang

不懂再问我