风中的刀!

月夜寒光,风中的刀!

My Links

Blog Stats

留言簿(162)

随笔分类

随笔档案

文章档案

相册


搜索

最新评论

阅读排行榜

评论排行榜

2006年12月31日 #

只言片语:关于检测 Internet 是否连接 等。

#include "stdafx.h"

#include <conio.h>
#include <Windows.h>
#include <Wininet.h>
#include <Sensapi.h>
#include <Ras.h.>
#include <Raserror.h>

#pragma comment(lib,"Wininet.lib")
#pragma comment(lib,"Sensapi.lib")
#pragma comment(lib,"Rasapi32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
 DWORD dwFlags;

 if( InternetGetConnectedState( &dwFlags,0 ) == FALSE ) {
  // Don't attempt connection or it will bring up the dialog
  // ...
  printf( "\nNot connected to Internet.\n\r" );
 }
 else {
  //Attempt connection
  //if( InternetOpenURL(...) == NULL ) {
  // // Call failed
  // err = GetLastError();
  // if ((err == ERROR_INTERNET_NAME_NOT_RESOLVED) ||
  //  (err == ERROR_INTERNET_CANNOT_CONNECT) ||
  //  (err == ERROR_INTERNET_TIMEOUT))
  // {
  //  // probably not connected...handle appropriately
  //  // ...
  // }
  //}
  // We're connected!!!
  // ....
  printf( "\n1) Has connected to Internet.\n\r" );
 }

 {
  if( dwFlags & INTERNET_CONNECTION_CONFIGURED )
   printf( "   Local system has a valid connection to the Internet, but it might or might not be currently connected.\n\r" );

  if( dwFlags & INTERNET_CONNECTION_LAN )
   printf( "   Local system uses a local area network to connect to the Internet.\n\r" );

  if( dwFlags & INTERNET_CONNECTION_MODEM )
   printf( "   Local system uses a modem to connect to the Internet.\n\r" );

  if( dwFlags & INTERNET_CONNECTION_MODEM_BUSY )
   printf( "   No longer used.\n\r" );

  if( dwFlags & INTERNET_CONNECTION_OFFLINE )
   printf( "   Local system is in offline mode.\n\r" );

  if( dwFlags & INTERNET_CONNECTION_PROXY )
   printf( "   Local system uses a proxy server to connect to the Internet.\n\r" );

  if( dwFlags & INTERNET_RAS_INSTALLED )
   printf( "   Local system has RAS installed.\n\r" );
 }

 {
  QOCINFO QOCInfo;
  BOOL bRet;
  TCHAR str[ 1024 ];

  QOCInfo.dwSize = sizeof( QOCINFO );
  bRet = IsDestinationReachable( "
http://www.163.com",&QOCInfo );
  if( bRet ) {
   printf( "2)
http://www.163.com is reachable.\n\r" );

   if( QOCInfo.dwFlags == NETWORK_ALIVE_LAN )
    printf( "   The computer has one or more active LAN cards.\n\r" );
   if( QOCInfo.dwFlags == NETWORK_ALIVE_WAN )
    printf( "   The computer has one or more active RAS connections.\n\r" );

   sprintf( str,"   Speed of data coming in: %d bytes/second\n\r",QOCInfo.dwInSpeed );
   printf( str );

   sprintf( str,"   Speed of data sent to: %d bytes/second\n\r",QOCInfo.dwOutSpeed );
   printf( str );
  }
  else {
   printf( "2)
http://www.163.com is not reachable.\n\r" );
  }
 }

 //{
 // DWORD dwCb = sizeof( RASCONN );
 // DWORD dwErr = ERROR_SUCCESS;
 // DWORD dwRetries = 5;
 // DWORD dwConnections = 0;
 // RASCONN* lpRasConn = NULL;

 // // Loop through in case the information from RAS changes between calls.
 // while( dwRetries-- ) {
 //  // If the memory is allocated, free it.
 //  if( NULL != lpRasConn ) {
 //   HeapFree( GetProcessHeap(),0,lpRasConn );
 //   lpRasConn = NULL;
 //  }
 //  // Allocate the size needed for the RAS structure.
 //  lpRasConn = ( RASCONN* )HeapAlloc( GetProcessHeap(),0,dwCb );
 //  if( NULL == lpRasConn ) {
 //   dwErr = ERROR_NOT_ENOUGH_MEMORY;
 //   break;
 //  }
 //  // Set the structure size for version checking purposes.
 //  lpRasConn->dwSize = sizeof( RASCONN );
 //  // Call the RAS API then exit the loop if we are successful or an unknown
 //  // error occurs.
 //  dwErr = RasEnumConnections( lpRasConn,&dwCb,&dwConnections );
 //  if( ERROR_BUFFER_TOO_SMALL != dwErr ) {
 //   break;
 //  }
 // }
 // // In the success case, print the names of the connections.
 // if( ERROR_SUCCESS == dwErr ) {
 //  DWORD i;
 //  printf( "The following RAS connections are currently active\n\r" );
 //  for( i = 0; i < dwConnections; i++ ) {
 //   printf( "%s\n\r",lpRasConn[i].szEntryName );
 //  }
 // }
 // else {
 //  printf( "RasEnumConnections failed: Error = %d\n\r",dwErr );
 // }
 // // Free the memory if necessary.
 // if( NULL != lpRasConn ) {
 //  HeapFree( GetProcessHeap(),0,lpRasConn );
 //  lpRasConn = NULL;
 // }
 //}

 {
  printf("3) IsNetworkAlive API\r\n");

  BOOL bRet;
  DWORD dwFlags;

  bRet = IsNetworkAlive( &dwFlags );
  if( bRet ) {
   if( dwFlags == NETWORK_ALIVE_LAN  )
    printf( "   The computer has one or more LAN cards that are active.\n\r" );
   if( dwFlags == NETWORK_ALIVE_WAN )
    printf( "   The computer has one or more active RAS connections.\n\r" );
   if( dwFlags == NETWORK_ALIVE_AOL )
    printf( "   The computer is connected to the America Online network.\n\r" );
  }
 }

 printf("4) Press any key to exit...\r\n");
 getch();

 return 0;
}

发表于 2006-12-31 11:08 风中的刀! 阅读(877) | 评论 (0)编辑 收藏