学院首页>网络编程>其它编程>检测计算机是否连网

检测计算机是否连网

作者:Netboy 来源:未知 添加时间:2006-5-21 11:17:30
利用WIN95中的注册表键值,在注册表的HKEY-LOCAL-MACHINE\System\CurrentControlSet\Services\RemoteAcces\下,当计算机连上因特网时,Remote Connection的植为01 00 00 00,反之,为00 00 00 00,通过这一建植可判断是否连网,接下来的事情就很简单了,只要在程序中(需用到API)去读取这个值就OK了,下面是程序代码。 
  
  
  
  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,lpcba 
  
  ta) 
  
  lpcbData=Len(lpData) 
  
  ReturnCode=RegQueryValueEx(hKey,lpValueName,lpReserved,lpType,ByVal lpData,lpcba 
  
  ta) 
  
  
  
  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 "你的计算机已经连接到Internet!" 
  
  else 
  
  msgbox "你的计算机还没有连接到Internet!" 
  
  end if 
  
  end sub 
站内搜索