 |
vb50
win98
我在学用VB50编程,现在需要检测是否与网络相连,使用MSCOMM控件可以检测到,可是先启动它后再启动调制器时显示调制器已经在使用。应该怎么使用?请指教,谢谢。
(gold)
|
| |
|
 |
在检测之后,应该使用PortOpen=False关闭Modem。
如何你要是想判断用户是否与Internet相连也可以这样做:
将下面的代码加入一个模块:
Public Const ERROR_SUCCESS = 0&
Public Const APINULL = 0&
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public ReturnCode As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias _
"RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long
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
Public Function ActiveConnection() As Boolean
Dim hKey As Long
Dim lpSubKey As String
Dim phkResult As Long
Dim lpValueName As String
Dim lpReserved As Long
Dim lpType As Long
Dim lpData As Long
Dim lpcbData As Long
ActiveConnection = False
lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, _
phkResult)
If ReturnCode = ERROR_SUCCESS Then
hKey = phkResult
lpValueName = "Remote Connection"
lpReserved = APINULL
lpType = APINULL
lpData = APINULL
lpcbData = APINULL
ReturnCode = RegQueryValueEx(hKey, lpValueName, _
lpReserved, lpType, ByVal lpData, lpcbData)
lpcbData = Len(lpData)
ReturnCode = RegQueryValueEx(hKey, lpValueName, _
lpReserved, lpType, lpData, lpcbData)
If ReturnCode = ERROR_SUCCESS Then
If lpData = 0 Then
ActiveConnection = False
Else
ActiveConnection = True
End If
End If
RegCloseKey (hKey)
End If
End Function
你就可以使用ActiveConnection进行检测了。
Private Sub Command1_Click()
If ActiveConnection = True Then
Call MsgBox("You have an active connection.", vbInformation)
Else
Call MsgBox("You have no active connections.", vbInformation)
End If
End Sub
另外,如果安装了IE 4.0以上版本,也可以使用InternetGetConnectedState函数判断是否与Internet相连,这种方法不仅可以判断拨号方式,也可以判断通过局域网连接。更详细的介绍参考微软的Knowledge Base的文章:“Q242558 HOWTO: Detecting If You Have a Connection to the Internet”。
相关问题:
QA003175 "如何能够判断网络已经断线"
QA000730 "用VB5写图形化PING 工具"
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, Internet编程, INET, www, internet, ie, 网络与通信, network, communicate, com, com1, com2。
|
| |
|
| |
|
| |
|
|