Office中国论坛/Access中国论坛

标题: 这个网络检测要那几个组件啊?(版主进) [打印本页]

作者: lixun005    时间: 2008-3-24 09:50
标题: 这个网络检测要那几个组件啊?(版主进)
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
Private Sub Command0_Click()
Dim Inet As Boolean
Dim eR As EIGCInternetConnectionState
Dim sName As String
    If Inet = False Then
        MsgBox "没有连接到Internet。"
        Exit Sub
    ElseIf Inet = True Then
        MsgBox "已经连接到Internet。"
        Exit Sub
    End If
End Sub
Private Sub Form_Load()
      Inet = InternetConnected(eR, sName)
  SocketsInitialize
End Sub

[ 本帖最后由 lixun005 于 2008-3-24 10:13 编辑 ]
作者: andymark    时间: 2008-3-24 10:21
你这个函数并不全呀 很多参数都没定义
作者: andymark    时间: 2008-3-24 10:26
检测是否已连接Internet 也可用下面的代码
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000

Private Declare Function InternetOpen _
Lib "wininet.dll" Alias "InternetOpenW" _
(ByVal lpszAgent As Long, _
ByVal dwAccessType As Long, _
ByVal lpszProxyName As Long, _
ByVal lpszProxyBypass As Long, _
ByVal dwFlags As Long) As Long

Private Declare Function InternetOpenUrl _
Lib "wininet.dll" Alias "InternetOpenUrlW" _
(ByVal hInet As Long, _
ByVal lpszUrl As Long, _
ByVal lpszHeaders As Long, _
ByVal dwHeadersLength As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long

Private Declare Function InternetCloseHandle _
Lib "wininet.dll" (ByVal hInet As Long) As Long

Public Function TestConnection(stSite As String) As Boolean
    Dim hInet As Long
    Dim hUrl As Long
    Dim lFlags As Long
    Dim url As Variant
   
    hInet = InternetOpen(StrPtr(Dir(CurrentDb.Name)), INTERNET_OPEN_TYPE_PRECONFIG, 0&, 0&, 0&)
    If hInet Then
        lFlags = INTERNET_FLAG_KEEP_CONNECTION Or _
                    INTERNET_FLAG_NO_CACHE_WRITE Or _
                    INTERNET_FLAG_RELOAD
        hUrl = InternetOpenUrl(hInet, StrPtr(stSite), 0&, 0, lFlags, 0)
        If hUrl Then
            TestConnection = True
            Call InternetCloseHandle(hUrl)
            hUrl = 0
         End If
    End If
    Call InternetCloseHandle(hInet)
    hInet = 0
End Function

Private Sub Command0_Click()
MsgBox TestConnection("http://www.google.com")
End Sub
作者: lixun005    时间: 2008-3-24 10:48
非常感谢,不过ping网站我这面没有合适的网站,公司屏蔽了好多网站[:47] ,google.com有时也打不开的,郁闷死了,不过还是谢谢你![:13]
作者: gdfsslec    时间: 2008-3-24 10:50
学习[:50]




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3