vb怎么判断网络是否已经连接上?

 我来答
sbj518
2016-01-07 · TA获得超过4.4万个赞
知道大有可为答主
回答量:1.1万
采纳率:81%
帮助的人:5292万
展开全部
GetHostbyName API可获取域名对应的IP地址,当没有联网状态将会返回一个错误值。所以可利用它来判断是否联网。

Private Sub Form_Load()
If IsConnectedState Then
MsgBox "连接网络"
Else
MsgBox "没有联网"
End If
End Sub

VBScript code复制代码

Option Explicit

Private Type WSADATA
wversion As Integer
wHighVersion As Integer
szDescription(0 To 256) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpszVendorInfo As Long
End Type

Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHostname As String) As Long
Private Const WS_VERSION_REQD = &H101

Public Function IsConnectedState() As Boolean
Dim udtWSAD As WSADATA
Call WSAStartup(WS_VERSION_REQD, udtWSAD)
IsConnectedState = CBool(gethostbyname("www.baidu.com"))
Call WSACleanup
End Function
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式