通过什么方法可以知道一台电脑是否在上网?

解决方案 »

  1.   

    BOOL YourClassHere::IsInternetConnected (void)
    {
     int nCheck = AfxSocketInit();
     CSocket m_Server;
     HKEY hKey;
     DWORD dwDial, dwDialType = REG_DWORD, dwDialSize = 4;
     DWORD dwNew = 0;
     BOOL bResult = true;
     
     if ( RegOpenKeyEx ( HKEY_CURRENT_USER,
        Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
         0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
        ; // We cannot find the key. Handle this situation or just continue
                                                    
     if ( RegQueryValueEx( hKey, "EnableAutodial", NULL, &dwDialType,
      (BYTE *) &dwDial, &dwDialSize ) != ERROR_SUCCESS )
      ; // We cannot find the value. Handle it. if ( dwDial ) { // We need to change the value, in order to make
                // a dialup window not to show up.
      
      if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
       dwDialType, (BYTE *) &dwNew, dwDialSize )) != ERROR_SUCCESS)
      ; // Failed? We shouldn't get here. You decide how to handle it
     }
      
     
     if ( !m_Server.Create() ) {                                                       
            // m_sError = _T( "Unable to create the socket." );
            bResult = false;
     }   // You can use www.microsoft.com in order to check whether DNS is available
    // or numeric IP otherwise 
     else if ( !m_Server.Connect( "www.microsoft.com", 80 ) ) {     //  207.46.130.150
            //m_sError = _T( "Unable to connect to server" );        
            m_Server.Close();
            bResult = false;
     }
     
     
     if ( dwDial ) {
      if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
       dwDialType, (BYTE *) &dwDial, dwDialSize )) != ERROR_SUCCESS)
      ; // Failed? We shouldn't get it. You decide how to handle this.
     }
     
     RegCloseKey( hKey );
     return ( bResult );
    }
      

  2.   

    BOOL bConnected=InternetGetConnectedState(m_dwflags,NULL);
    if (bConnected)//如果上网则为真
    {
       return ture;
    }需包含#include "Wininet.h" 和 链接Wininet.lib