BOOL IsInternetConnected()
In some occasions you can try to use RasEnumConnections, in most cases on a typical computer opened Ras Connection mean that the user is connected to the internet. However, this idea won't work in case the user is connected to internet not via Ras device or is connected to some kind of local network.
We try to use common sence here. Since internet is something abstract, lets decide that user which is connected to internet, is the one who can connect to www.microsoft.com.
First I wanted to use ping, but found a lot of troubles with it. Therefore, I have decided to use sockets "directly". I didn't want to write some fance code here, rather I have wrote a simple function you can cut & paste into your application.
Another problem adressed in the function that in some computer a try to establish internet connection will bring dialup dialog. The function solve it by temporary disabling the feature in the registry and enabling it back afterwards.
Pay attention to the comments.
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 );
}
I would like to thank Martin Tschepe for pointing me out this registry key.
History

Comments
Nike Current Max 1 FB release, father a eager color grain, the chic shoes
Posted by Geozyoceada on 04/19/2013 05:02amIn the summer in a tumbler interior the cool sprite seems to be a decorous choice, but if the sprite "feet"? Resolution also supply you a set off, bring a invigorating! This summer, Nike and Sprite [url=http://northernroofing.co.uk/roofins.cfm]nike free run uk[/url] and his sneakers to a commingling of classic snow spread of unripened, white and indecent color scheme in the definitive Nike Superciliousness Max 1 shoes let it be known a refreshing cool scent.[url=http://northernroofing.co.uk/roofins.cfm]nike free run uk[/url] Summer is the yet to select a cleanly shoe, shoes should be a obedient choice. Qualifying series Nike Publicize Max HomeTurf metropolis recently lastly comes up, this series in the classic Breath Max shoes to London, Paris and Milan the three paid glorification to the iconic megalopolis of Europe, combined with the characteristics of the three cities, Sense Max 1 HYP,Allied Max 90 HYP,Show Max 1 and shoes such as Make public Max 95, combined [url=http://northernroofing.co.uk/roofins.cfm]nike free run uk[/url] with the Hyperfuse, as well as a heterogeneity of materials, such as suede, Whether you crave going or retro-everything.
ReplyprXujV Xr RE GOi RgfQ JS
Posted by UpSwmXwvss on 11/02/2012 02:01pmklonopin price klonopin dosage weight - klonopin side effects and breastfeeding
ReplyAn easier alternative
Posted by Legacy on 08/11/2003 12:00amOriginally posted by: Patrick Philippot
-
-
ReplyAbsolutely Brilliant
Posted by bob16972 on 03/26/2005 06:19pmYour solution should have been included in "Visual C++ Goodies" instead of "Function To Verify if Connected to the internet" methinks. Too bad the book came out first. Thanks Patrick (and Edwin) for sharing it
ReplyThanks
Posted by skjacob on 02/11/2005 01:06amUr solution works perfectly
ReplyWinsock Proxy handling
Posted by Legacy on 01/20/2003 12:00amOriginally posted by: Eugene Polonsky
Using Winsock to check connection status--has anyone run into any issues regarding proxy server settings when attempting to verify connection?
ReplyError Message when use the function "InetIsOffline"
Posted by Legacy on 04/02/2002 12:00amOriginally posted by: Maverick
-
ReplyReply for Error Message when use the function "InetIsOffline"
Posted by skjacob on 02/11/2005 12:59amAdd url.lib to ur settings\Link tab
ReplyError in WININET.H
Posted by Legacy on 12/27/2001 12:00amOriginally posted by: Rey
When I compile wininet.h I get an error with LPVOID, can anyone help?
Replydetecting internet status in VJ++
Posted by Legacy on 11/13/2001 12:00amOriginally posted by: Priyanka
ReplyWindows ME?
Posted by Legacy on 09/30/2001 12:00amOriginally posted by: Phil Hord
I've heard from a user that autodial is erroneously triggered on Windows ME. Does anyone have any info on WinME registry settings to disable AutoDial?
ReplyNo existe el archivo
Posted by Legacy on 08/08/2001 12:00amOriginally posted by: V�ctor Lema
no 'ta el archivo .zip
ReplyFix for the long delay
Posted by Legacy on 07/24/2001 12:00amOriginally posted by: Rob Adamson
ReplyLoading, Please Wait ...