Search This Blog

Friday, July 17, 2009

How to get Internet Connection State using Win32 API

Intro.

There are many situations in which we need to check up for the Internet connection state of the local computer in which our application is running. I've seen so many people asking about this question in many forums.. and i've als got this query from some more people. Actually, its not heavy task as many people thinks :)

How To.
Single call to a Windows API with an if statement can do it for you :)


The InternetGetConnectedState() in Windows takes two parameters.. one is a Pointer to a variable that receives the connection description, and the other is a reserved DWORD value which always must be Zero.

Code.

void CheckInetStat( )
{
DWORD dwFlags;
if( InternetGetConnectedState( &dwFlags, NULL ) )
{



if ( dwFlags & INTERNET_CONNECTION_CONFIGURED )
{
MessageBox( hWnd, _T( "This System has a valid Internet Connection, but it might or might not be currently connected." ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_CONNECTION_LAN)
{
MessageBox( hWnd, _T( "This System uses LAN for Internet Connection" ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_CONNECTION_MODEM)
{
MessageBox( hWnd, _T( "This System is using a Modem for Internet Connection" ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_CONNECTION_MODEM_BUSY )
{ //No longer used.
MessageBox( hWnd, _T( "The 'Modem is Busy" ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_CONNECTION_OFFLINE)
{
MessageBox( hWnd, _T( "This System is currently in Offline Mode" ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_CONNECTION_PROXY)
{
MessageBox( hWnd, _T( "This System is using Proxy Server for Internet" ), _T( "Internet Status" ), MB_OK );
}



if ( dwFlags & INTERNET_RAS_INSTALLED)
{
MessageBox( hWnd, _T( "This system has RAS Installed" ), _T( "Internet Status" ), MB_OK );
}


} //End of Function.




PS : I love to hear from you. So, please leave Feedbacks and Comments and help me Improve :-)

3 comments:

  1. A nice one Thanks for sharing

    ReplyDelete
  2. It is taking time to return false value. Is there any solution ?

    ReplyDelete
  3. Cool and that i have a keen present: Where Do You Get The Money To Renovate A House home renovation youtube

    ReplyDelete