VB中检测是否连网

发表于:2007-06-21来源:作者:点击数: 标签:
利用WIN95中的注册表建植,在注册表的HKEY-LOCAL-MACHINE\System\CurrentControlSet\Services\RemoteAcces\下,当计算机连上因特网时,Remote Connection的植为01 00 00 00,反之,为00 00 00 00,通过这一建植可判断是否连网。 1.建立新模块 Public Const E

   
  利用WIN95中的注册表建植,在注册表的HKEY-LOCAL-MACHINE\System\CurrentControlSet\Services\RemoteAclearcase/" target="_blank" >cces\下,当计算机连上因特网时,Remote Connection的植为01 00 00 00,反之,为00 00 00 00,通过这一建植可判断是否连网。
1.建立新模块

Public Const ERROR-SUCCESS= 0&
Public Const APINULL= 0&
Public Const HKEY-LOCAL-MACHINE= &H80000002
Public ReturnCode As long
’声明API函数
RegCloseKey()
RegQueryValueEx()

’自定义函数
Public function ActiveConnection() As Boolean

  Dim hKey As long
  Dim lpSubKey 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,lpcbata)
   lpcbData=Len(lpData)
   ReturnCode=RegQueryValueEx(hKey,lpValueName,lpReserved,lpType,ByVal lpData,lpcbata)

   If ReturnCode=ERROR-SUCCESS then
    If lpData=0 then
       ActiveConnection=False
     Else
       ActiveConnection=True
     End If
   End If
   RegCloseKey(hKey)
 End If
End funtion

2.新建窗体
Priate sub Command1_click()
 If ActiveConnection=True then
  MsgBox "OK!"
 Else
  MsgBox "ERROR!"
 End If
End sub

原文转自:http://www.ltesting.net