Option Explicit
'
' Required WinInet.dll declarations
'
Private Declare Function InternetAutodial Lib "wininet.dll" (ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodialHangup Lib "wininet.dll" (ByVal dwReserved As Long) As Long
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (lpdwFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, 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
'
' // Flags for InternetAutodial
'
Private Const INTERNET_AUTODIAL_FORCE_ONLINE = 1 ' Forces an online Internet connection.
Private Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2 ' Forces an unattended Internet dial-up.
Private Const INTERNET_AUTODIAL_FAILIFSECURITYCHECK = 4 ' Essentially undocumented???
' Indicates to use config info from registry
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
' WinInet flags
Private Const INTERNET_FLAG_RELOAD = &H80000000 'read from wire even if locally cached
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000'// use keep-alive semantics
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000'// don't write this item to the cache
'
' Enumerated dial modes
'
Public Enum AutodialFlags
InternetAutodialForceOnline = INTERNET_AUTODIAL_FORCE_ONLINE
InternetAutodialUnattended = INTERNET_AUTODIAL_FORCE_UNATTENDED
InternetAutodialFailIfSecurityCheck = INTERNET_AUTODIAL_FAILIFSECURITYCHECK
End Enum
'
' // Flags for InternetGetConnectedState
'
Private Const INTERNET_CONNECTION_MODEM = 1
Private Const INTERNET_CONNECTION_LAN = 2
Private Const INTERNET_CONNECTION_PROXY = 4
Private Const INTERNET_CONNECTION_MODEM_BUSY = 8
'
' Enumerated connection states
'
Public Enum InetConnectionStates
InternetConnectionModem = INTERNET_CONNECTION_MODEM
InternetConnectionLan = INTERNET_CONNECTION_LAN
InternetConnectionProxy = INTERNET_CONNECTION_PROXY
InternetConnectionModemBusy = INTERNET_CONNECTION_MODEM_BUSY
End Enum
'
' Set aside storage for private member variables.
'
Private m_Connected As Boolean
Private m_ConnectMode As InetConnectionStates
Private m_ConnectModeDesc As String
Private m_KDatenSites As Collection
' ********************************************
' Initialize
' ********************************************
Private Sub Class_Initialize()
Set m_KDatenSites = New Collection
' Populate the collection with what *you* feel are reliable sites!
m_KDatenSites.Add "http://www.baidu.com", "http://www.baidu.com"
m_KDatenSites.Add "http://www.163.com", "http://www.163.com"
End Sub
Private Sub Class_Terminate()
Set m_KDatenSites = Nothing
DoEvents
End Sub
' ********************************************
' Public Properties
' ********************************************
Public Property Get KDatenTestSites() As Collection
Set KDatenTestSites = m_KDatenSites
DoEvents
End Property
Public Property Let KDatenTestSites(ByVal NewVal As Collection)
Set m_KDatenSites = NewVal
DoEvents
End Property
' ********************************************
' Public Properties // Read-Only
' ********************************************
Public Property Get Connected() As Boolean
Call Me.Refresh
DoEvents
Connected = m_Connected
End Property
Public Property Get ConnectMode() As InetConnectionStates
Call Me.Refresh
DoEvents
ConnectMode = m_ConnectMode
End Property
Public Property Get ConnectModeDesc() As String
Call Me.Refresh
DoEvents
ConnectModeDesc = m_ConnectModeDesc
End Property
' ********************************************
' Public Methods
' ********************************************
Public Function Dial(Optional Flags As AutodialFlags = InternetAutodialUnattended) As Boolean
Dial = False
If Not Connected Then
DoEvents
If CBool(InternetAutodial(Flags, 0&)) Then
DoEvents
Dial = True
Else
DoEvents
End If
End If
End Function
Public Function HangUp() As Boolean
If Connected Then
DoEvents
HangUp = CBool(InternetAutodialHangup(0&))
Else
DoEvents
HangUp = False ' maybe should be True???
End If
End Function
Public Sub Refresh()
Dim Flags As Long
m_Connected = InternetGetConnectedState(Flags, 0&)
m_ConnectMode = Flags
If Flags And INTERNET_CONNECTION_MODEM Then
DoEvents
m_ConnectModeDesc = "Modem"
If Flags And INTERNET_CONNECTION_MODEM_BUSY Then
DoEvents
m_ConnectModeDesc = "Modem (Busy)"
m_Connected = TryTheWire()
End If
ElseIf Flags And INTERNET_CONNECTION_LAN Then
DoEvents
m_ConnectModeDesc = "LAN"
m_Connected = TryTheWire()
ElseIf Flags And INTERNET_CONNECTION_PROXY Then
DoEvents
m_ConnectModeDesc = "Proxy Server"
m_Connected = TryTheWire()
End If
End Sub
' ********************************************
' Private Methods
' ********************************************
Private Function TryTheWire() As Boolean
Dim hInet As Long
Dim hUrl As Long
Dim Flags As Long
Dim url As Variant
'
' Try opening each URL in the collection, until one succeeds.
'
hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
If hInet Then
DoEvents
Flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE Or INTERNET_FLAG_RELOAD
For Each url In m_KDatenSites
DoEvents
hUrl = InternetOpenUrl(hInet, CStr(url), vbNullString, 0, Flags, 0)
If hUrl Then
DoEvents
Call InternetCloseHandle(hUrl)
TryTheWire = True
Exit For
End If
Next url
End If
DoEvents
Call InternetCloseHandle(hInet)
End Function
第二步:在你的代码中:
'判断是否连接上Internet
Set cn = New CNetConnect
'如果是脱机状态:
If cn.Connected = False Then
msgbox "没联网,执行你的shell吧"
else
msgbox "已经联网,想干什么干什么吧"
End If
PS:类模块中的网址:“http://www.baidu.com,http://www.baidu.com”,是可以随意换的。它的作用是找一个稳定的、始终在线的主机,用来判断是否可以连接得通,也是这个类代码的核心算法所在。
静态路由和动态路由的区别:静态路由一般适用于比较简单的网络;动态态路由应用在复杂的网络环境
具体区别:
1、动态路由是与静态路由相对的一个概念,指路由器能够根据路由器之间的交换的特定路由信息自动地建立自己的路由表,并且能够根据链路和节点的变化适时地进行自动调整。
当网络中节点或节点间的链路发生故障,或存在其它可用路由时,动态路由可以自行选择最佳的可用路由并继续转发报文。
2、使用静态路由的好处是网络安全保密性高。动态路由因为需要路由器之间频繁地交换各自的路由表,而对路由表的分析可以揭示网络的拓扑结构和网络地址等信息。
因此,网络出于安全方面的考虑也可以采用静态路由。不占用网络带宽,因为静态路由不会产生更新流量。
拓展资料静态路由一般适用于比较简单的网络环境,在这样的环境中,网络管理员易于清楚地了解网络的拓扑结构,便于设置正确的路由信息。
在一个支持DDR(Dial-on-Demand Routing)的网络中,拨号链路只在需要时才拨通,因此不能为动态路由信息表提供路由信息的变更情况。在这种情况下,网络也适合使用静态路由。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)