Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

检测网络联接状态

时 间:2009-03-19 22:15:43
作 者:外网转载   ID:2469  城市:东莞
摘 要:在应用系统中一直想有一个方法能够检测当前电脑联接状态,今天在外网朋友那边搜到了这个API函数,试了一下不错!
供大家参考。
正 文:

'检测本机是否联入互联网,以及以什么形式联入

Option Explicit
'检测本机是否联入互联网,以及以什么形式联入
Dim Ts_ShenJun 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

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

    On Error Resume Next

    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 Function IsInNet() As Boolean

'检测是否已经以及使用什么方法连接到Internet
    Dim bConnected As Boolean
    Dim eR As Long
    Dim sName As String
    Dim sMsg As String

    bConnected = InternetConnected(eR, sName)

    '根据获得的结果输出
    If (eR And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM Then
        sMsg = sMsg & "使用 Modem 连接到 Internet!" & Chr(10) & Chr(13) & Chr(13)
    End If

    If (eR And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then
        sMsg = sMsg & "使用内部网连接到 Internet!" & Chr(10) & Chr(13) & Chr(13)
    End If

    If (eR And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then
        sMsg = sMsg & "通过代理服务器连接到 Internet!" & Chr(10) & Chr(13) & Chr(13)
    End If

    If (eR And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then
        sMsg = sMsg & "现在连接处于离线状态!" & Chr(10) & Chr(13) & Chr(13)
    End If

    If (eR And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then
        sMsg = sMsg & "连接已经被设定!" & Chr(10) & Chr(13) & Chr(13)
    Else
        sMsg = sMsg & "没有设定好的连接!" & Chr(10) & Chr(13) & Chr(13)
    End If

    If (eR And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then
        sMsg = sMsg & "本机已经安装了远程访问服务功能!"
    End If

    Ts_ShenJun = MsgBox(sMsg, 48, "联接状态")

    IsInNet = bConnected

End Function


Sub Main()
    If IsInNet Then
        MsgBox "connected!"
    Else
        MsgBox "no   connected!"
    End If
End Sub

'模块中调用 IsInNet 过程即可



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助