Click here to Skip to main content
15,889,315 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Memory DC Pin
Dethulus1-Feb-06 1:18
Dethulus1-Feb-06 1:18 
GeneralRe: Memory DC Pin
Ryan Binns1-Feb-06 1:23
Ryan Binns1-Feb-06 1:23 
QuestionHow to know programmatically if network cable is connected or not? Pin
Amarelia31-Jan-06 2:22
Amarelia31-Jan-06 2:22 
AnswerRe: How to know programmatically if network cable is connected or not? Pin
David Crow31-Jan-06 3:27
David Crow31-Jan-06 3:27 
GeneralRe: How to know programmatically if network cable is connected or not? Pin
Amarelia1-Feb-06 8:05
Amarelia1-Feb-06 8:05 
AnswerRe: How to know programmatically if network cable is connected or not? Pin
M.Mehrdad.M31-Jan-06 4:05
M.Mehrdad.M31-Jan-06 4:05 
GeneralRe: How to know programmatically if network cable is connected or not? Pin
Amarelia1-Feb-06 8:07
Amarelia1-Feb-06 8:07 
AnswerRe: How to know programmatically if network cable is connected or not? Pin
bob1697231-Jan-06 8:43
bob1697231-Jan-06 8:43 
An ISP connection does not mean the network cable is plugged in but would provide you with similar capabilities. I am assuming you want to see if you have network connectivity which requires you take any IP capable connection into consideration. Checking the routing table for a default route is the best technique I've come across so far as it does not require checking a URL, pinging an IP address, or other techniques that will not be entirely accurate in all contexts.

I got the idea from someone on CodeGuru before they upgraded their website and lost all the legacy comments. Unfortunately, I do not know the name of the person who passed on the idea so I can't give credit where credit is do but I'll pass it on...

The function to use is GetIpForwardTable(). I use a timer to periodically check for connectivity and update my status indicator accordingly. There is a slight delay maybe a second or two if you unplug the cable or plug it in (assuming your timer is set to fire every second). No noticeable delay when disabling the connection using a GUI tool.

Don't forget to include in StdAfx.h...
#include <iphlpapi.h> // For Routing table query

And make sure to reference the Platform SDK lib file...
iphlpapi.lib

BOOL CNetworkConnectionDlg::IsInternetAvailable(void)
{
MIB_IPFORWARDTABLE * pRoutingTable;
DWORD dwBufferSize=0;
BOOL bIsInternetAvailable=FALSE;
DWORD dwResult;
DWORD dwIndex;
DWORD dwRowCount;

// Get the required buffer size
GetIpForwardTable(NULL,&dwBufferSize,FALSE);
pRoutingTable=(MIB_IPFORWARDTABLE*)new BYTE[dwBufferSize];

// Attempt to fill buffer with routing table information
dwResult=GetIpForwardTable(pRoutingTable,&dwBufferSize,FALSE);

if (dwResult==NO_ERROR) {
dwRowCount=pRoutingTable->dwNumEntries; // Get row count

// Look for default route to gateway
for (dwIndex=0;dwIndex<dwRowCount;dwIndex++) {
if (pRoutingTable->table[dwIndex].dwForwardDest==0) { // Default route designated by 0.0.0.0 in table
bIsInternetAvailable=TRUE; // Found it
break; // Short circuit loop
}
}
}

delete pRoutingTable; // Clean up. Just say "No" to memory leaks

return bIsInternetAvailable;
}
GeneralRe: How to know programmatically if network cable is connected or not? Pin
Amarelia1-Feb-06 8:09
Amarelia1-Feb-06 8:09 
JokeRe: How to know programmatically if network cable is connected or not? Pin
Balon Fan22-Jun-08 1:43
Balon Fan22-Jun-08 1:43 
QuestionChanging Views in SplitterWindow-Panes Pin
hever31-Jan-06 2:04
hever31-Jan-06 2:04 
QuestionRe: Changing Views in SplitterWindow-Panes Pin
David Crow31-Jan-06 3:34
David Crow31-Jan-06 3:34 
GeneralRe: Changing Views in SplitterWindow-Panes Pin
hever31-Jan-06 7:52
hever31-Jan-06 7:52 
QuestionRe: Changing Views in SplitterWindow-Panes Pin
hever31-Jan-06 8:46
hever31-Jan-06 8:46 
AnswerRe: Changing Views in SplitterWindow-Panes Pin
hever31-Jan-06 10:44
hever31-Jan-06 10:44 
Questionset dialog to no max or no change of size Pin
waxie31-Jan-06 1:36
waxie31-Jan-06 1:36 
AnswerRe: set dialog to no max or no change of size Pin
Rage31-Jan-06 2:10
professionalRage31-Jan-06 2:10 
AnswerRe: set dialog to no max or no change of size Pin
toxcct31-Jan-06 2:20
toxcct31-Jan-06 2:20 
Questionrunning and terminating EXEs automatically Pin
Sasikumar.SR31-Jan-06 1:28
Sasikumar.SR31-Jan-06 1:28 
AnswerRe: running and terminating EXEs automatically Pin
Rage31-Jan-06 1:41
professionalRage31-Jan-06 1:41 
GeneralRe: running and terminating EXEs automatically Pin
David Crow31-Jan-06 3:36
David Crow31-Jan-06 3:36 
GeneralRe: running and terminating EXEs automatically Pin
Rage31-Jan-06 5:09
professionalRage31-Jan-06 5:09 
GeneralRe: running and terminating EXEs automatically Pin
Sasikumar.SR31-Jan-06 19:02
Sasikumar.SR31-Jan-06 19:02 
GeneralRe: running and terminating EXEs automatically Pin
Sasikumar.SR31-Jan-06 22:18
Sasikumar.SR31-Jan-06 22:18 
GeneralRe: running and terminating EXEs automatically Pin
David Crow1-Feb-06 2:20
David Crow1-Feb-06 2:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.