'检测Internet网络连接状态

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Option Explicit

'检测Internet网络连接状态

'eg. bConnected = InternetConnected(eR, sName) As Boolean

'InternetConnectionState - API函数定义

Dim eR As EIGCInternetConnectionState

Dim sMsg As String

Dim sName As String

Dim bConnected As Boolean

Private Declare Function InternetSetDialState Lib "wininet.dll" _

(ByVal lpszConnectoid As String, ByVal dwState As Long, _

ByVal dwReserved As Long) As Long

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _

(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _

ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _

Alias "InternetGetConnectedStateExA" _

(ByRef lpdwFlags As Long, _

ByVal lpszConnectionName As String, _

ByVal dwNameLen As Long, _

ByVal dwReserved As Long _

) As Long

Private Enum EIGCInternetConnectionState

INTERNET_CONNECTION_MODEM = &H1&

INTERNET_CONNECTION_LAN = &H2&

INTERNET_CONNECTION_PROXY = &H4&

INTERNET_RAS_INSTALLED = &H10&

INTERNET_CONNECTION_OFFLINE = &H20&

INTERNET_CONNECTION_CONFIGURED = &H40&

End Enum

'InternetConnected 函数判断是否连接到Internet的函数

'获得是否以及通过何中方式连接到Internet上

Private Function InternetConnected(Optional ByRef eConnectionInfo _

As EIGCInternetConnectionState, Optional ByRef _

sConnectionName As String) As Boolean

Dim dwFlags As Long

Dim sNameBuf As String

Dim lR As Long

Dim iPos As Long

sNameBuf = String$(513, 0)

lR = InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&)

eConnectionInfo = dwFlags

iPos = InStr(sNameBuf, vbNullChar)

If iPos > 0 Then

sConnectionName = Left$(sNameBuf, iPos - 1)

ElseIf Not sNameBuf = String$(513, 0) Then

sConnectionName = sNameBuf

End If

InternetConnected = (lR = 1)

End Function

Public Sub ICS()

Dim sMsg As String

'InternetConnected 函数的调用

'检测是否已经连接到Internet

bConnected = InternetConnected(eR, sName)

sMsg = "网络连接正常"

If (eR And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then

sMsg = sMsg & "本机已经安装了远程访问服务功能." & vbCrLf

End If

'显示连接名称

If bConnected Then

MsgBox sMsg

Else

MsgBox "您尚未连接网络!"

End If

End Sub

相关文档
最新文档