7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Type QOCINFO
dwSize As Long
dwFlags As Long
dwInSpeed As Long
dwOutSpeed As Long
End Type
Private Const NETWORK_ALIVE_LAN As Long = &H1
Private Const NETWORK_ALIVE_WAN As Long = &H2
Private Const NETWORK_ALIVE_AOL As Long = &H4
Private Declare Function IsDestinationReachable Lib "sensapi.dll" _
Alias "IsDestinationReachableA" _
(ByVal lpszDestination As String, _
lpQOCInfo As QOCINFO) As Long
Private Const REG_MULTI_SZ = 7&
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_TCPIP As String = "SYSTEM\CurrentControlSet\Services\Tcpip\"
Private Declare Function RegOpenKey Lib "advapi32.dll" _
Alias "RegOpenKeyA" _
(ByVal HKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" _
(ByVal HKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As Any, _
lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal HKey As Long) As Long
Public Function GetLocalIP() As String
Dim HKey&, strBuf As String, DataBufSize As Long, data$, Subkey$
On Error Resume Next
'获得配置ip信息的子键信息
RegOpenKey HKEY_LOCAL_MACHINE, HKEY_TCPIP & "Linkage", HKey
'读取缓冲区大小
RegQueryValueEx HKey, "Route", 0, REG_MULTI_SZ, ByVal 0, DataBufSize
'建立缓冲区
strBuf = String$(DataBufSize, Chr$(0))
RegQueryValueEx HKey, "Route", 0, REG_MULTI_SZ, ByVal strBuf, DataBufSize
'获取Chr(0)分隔的键值
data = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
'过滤一个"
Subkey = Mid$(data, 2, Len(data) - 2)
'获得IP地址
RegOpenKey HKEY_LOCAL_MACHINE, HKEY_TCPIP & "Parameters\Interfaces\" & Subkey, HKey
RegQueryValueEx HKey, "IPAddress", 0, REG_MULTI_SZ, ByVal 0, DataBufSize
strBuf = String$(DataBufSize, Chr$(0))
RegQueryValueEx HKey, "IPAddress", 0, REG_MULTI_SZ, ByVal strBuf, DataBufSize
GetLocalIP = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End Function
Public Function IsNetConnectionAlive(ByVal localIP As String) As Boolean
Dim result As Long
Dim qoc As QOCINFO
qoc.dwSize = Len(qoc)
result = IsDestinationReachable(localIP, qoc) '测试本地IP
IsNetConnectionAlive = qoc.dwFlags
End Function
Private Sub Command1_Click()
If IsNetConnectionAlive(GetLocalIP) Then
MsgBox "已连接"
Else
MsgBox "连接断开"
End If
End Sub
Private Declare Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" (ByVal hInternet As Long, ByVal dwOption As Long, lpBuffer As Any, ByVal dwBufferLength As Long) As Long
Private Type internetInfo
lngconnectedstate As Long
lngFlags As Long
End Type
Private Sub Test()
Dim connectInfo As internetInfo
Dim lngSuccess As Long
connectInfo.lngconnectedstate = &H10 '断开连接
connectInfo.lngFlags = 1 '设置标识
lngSuccess = InternetSetOption(0, 50, connectInfo, Len(connectInfo))
If lngSuccess = 1 Then
MsgBox "网络断开成功", vbInformation, "测试"
Else
MsgBox "网络断开失败", vbInformation, "测试"
End If
End Sub
Private Sub Command1_Click()
Call Test
End Sub