五种查询Internet连接状态的方法

来源:互联网 发布:淘宝店铺号怎么隐藏 编辑:程序博客网 时间:2024/06/05 12:49

五种查询Internet连接状态[含IP]的方法    
  1.Powersock   控件法:  
  这种方法最简单,利用FastNet页的   Powersock控件的LocalIP属性即可判断:  
  if(Powersock1->LocalIP=="127.0.0.1"):在线  
  else:离线  
  特点:[1]判断连接状态,[2]获得本地IP。  
   
  2.使用URL.DLL的InetIsOffline(0)   函数:  
  Win2K:URL.DLL存放在/SYSTEM32/;  
  Win9x:URL.DLL存放在/SYSTEM/;  
  用GetSystemDirectory(...)得到系统目录。  
  InetIsOffline(0)返回值:  
  TRUE:   离线;   FALSE:在线。  
  特点:判断连接状态。  
   
  3.WinSock编程法:见程序  
  特点:[1]判断连接状态;[2]获得本地IP和主机名。  
   
  4.WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:  
  注意:为使用该函数,须在项目文件中加入:USELIB("WinInet.LIB")  
  特点:获得较详的连接描述!  
   
  5.RASAPI32.DLL的RasEnumConnections函数:  
  要使用该“枚举所有活动连接”函数,必须:  
  #include   "ras.h"。    
   
  若连接数>0:本机当前已连入Internet;  
  否则:   本机当前未连入Internet;  
   
  源码如下,在[BCB5   +   WIN2K   +   拨号上网]下通过(N字头的为菜单项):  
   
  -------------Powersock控件法-----------------------------------------  
  void   __fastcall   TForm1::N11Click(TObject   *Sender)    
  {  
  if(Powersock1->LocalIP=="127.0.0.1")  
  ShowMessage("未连接:"+Powersock1->LocalIP);  
  else   ShowMessage("已连接:"+Powersock1->LocalIP);  
  }    
   
   
  -------------URL.DLL的InetIsOffline函数法----------------------------  
  HINSTANCE   hDLL;  
  typedef   bool   __stdcall(*FUN)(int);   定义DLL函数指针FUN  
  FUN   isOffLine;    
  void   __fastcall   TForm1::N21Click(TObject   *Sender)  
  {  
  char   Buffer[MAX_PATH];  
  GetSystemDirectory(Buffer,MAX_PATH);  
  hDLL=LoadLibrary((AnsiString(Buffer)+"//URL.DLL").c_str());  
  if(hDLL==NULL){

        ShowMessage("Cannot   load   URL.DLL!   Return...   "); 

       return;

  }  
  isOffLine=(FUN)GetProcAddress(hDLL,"InetIsOffline");  
  if(isOffLine==NULL)

         ShowMessage("Cannot   load   InetIsOffline(int),   Return...");  

         return;

   }  
  if(!isOffLine(0))   ShowMessage("已连接");  
  else   ShowMessage("未连接");  
  FreeLibrary(hDLL);  
  }    
   
   
  ------------WinSock法------------------------------------------------  
  void   __fastcall   TForm1::N31Click(TObject   *Sender)    
  {  
  WORD   wVersionRequested;  
  WSADATA   wsaData;  
  wVersionRequested=MAKEWORD(1,1);   Start   up   WinSock  
  WSAStartup(wVersionRequested,&wsaData);  
  -----------------------------------------  
  hostent   *p;   char   *p2;   char   s[128];  
  gethostname(s,128);   Get   the   computer   name  
  p=gethostbyname(s);  
  p2=inet_ntoa(*((in_addr   *)p->h_addr));   Get   the   IpAddress  
  -----------------------------------------  
  AnsiString   LocationIP=p2;  
  if(LocationIP=="127.0.0.1")  
  ShowMessage("未连接:"+LocationIP);  
  else   ShowMessage("已连接:"+LocationIP);  
  WSACleanup();  
  }    
   
   
  -----------WinInet.DLL的InternetGetConnectedState函数法----------------  
  void   __fastcall   TForm1::N41Click(TObject   *Sender)    
  {  
  StaticText1->Caption="";   StaticText2->Caption="";   StaticText3->Caption="";  
  StaticText4->Caption="";   StaticText5->Caption="";   StaticText6->Caption="";  
  StaticText7->Caption="";  
  DWORD   dwFlag;  
  InternetGetConnectedState(&dwFlag,0);  
  if(dwFlag   &   INTERNET_CONNECTION_MODEM)  

            StaticText1->Caption="Yes";   MODEM连接  
  else   StaticText1->Caption="No"; 

  
  if(dwFlag   &   INTERNET_CONNECTION_LAN) 

           StaticText2->Caption="Yes";   LAN连接  
  else   StaticText2->Caption="No";  


  if(dwFlag   &   INTERNET_CONNECTION_PROXY)  

            StaticText3->Caption="Yes";   代理连接  
  else   StaticText3->Caption="No";  
  ---------检查是否连接-------------------------------------------  
  if(InternetGetConnectedState(NULL,0))  

            StaticText4->Caption="Yes";   在线  
  else   StaticText4->Caption="No";  


  if(dwFlag   &   INTERNET_CONNECTION_OFFLINE) 

  StaticText5->Caption="Yes";//离线。注:不好用!  
  else   StaticText5->Caption="No";  
  ----------------------------------------------------------------  
  if(dwFlag   &   INTERNET_RAS_INSTALLED)  

            StaticText6->Caption="Yes";  
  else   StaticText6->Caption="No";  


  if(dwFlag   &   INTERNET_CONNECTION_CONFIGURED) 

            StaticText7->Caption="Yes";  
  else   StaticText7->Caption="No";  
  }  
  ----------RASAPI32.DLL的RasEnumConnections函数法---------------------------  
  #include   "ras.h"  
  void   __fastcall   TForm1::N51Click(TObject   *Sender)    
  {  
  RASCONN   RASconn[256];   活动连接数组  
  DWORD   BuffSize;   数组所占内存大小;  
  DWORD   ConnNum;   活动连接数目  
  RASconn[0].dwSize=sizeof(RASCONN);   必须指定一个连接[数组元素]的内存大小;  
  BuffSize=sizeof(RASCONN)*256;  
  DWORD   dwReturn=RasEnumConnections(RASconn,&BuffSize,&ConnNum);  
  if(dwReturn==0)  
  {  
  if(ConnNum>0)   ShowMessage("已连接。当前激活连接数:"+AnsiString(ConnNum));  
  else   ShowMessage("未连接。当前激活连接数:"+AnsiString(ConnNum));  
  }  
  else   ShowMessage("RasEnumConnections函数失败!");  
  }    

原创粉丝点击