Hi every body,
I have a problem when i detect network connection in Windows has install VirtualBox and VMWare.
This is my code:
bool MainWindow::IsNetworkConnected()
{
bool bReturn = false;
QList<QString> possibleMatches;
if ( !ifaces.isEmpty() )
{
for(int i=0; i < ifaces.size(); i++)
{
unsigned int flags = ifaces[i].flags();
// If this interface isn't running, we don't care about it
if ( !isRunning )
continue;
// We only want valid interfaces that aren't loopback/virtual and not point to point
if ( !ifaces[i].isValid() || isLoopback || isP2P || !isUp)
continue;
QList<QHostAddress> addresses = ifaces[i].allAddresses();
for(int a=0; a < addresses.size(); a++)
{
// Ignore local host
// Ignore non-ipv4 addresses
if ( !addresses[a].toIPv4Address() )
continue;
QString ip
= addresses
[a
].
toString();
if ( ip.isEmpty() )
continue;
bool foundMatch = false;
for (int j=0; j < possibleMatches.size(); j++)
if ( ip == possibleMatches[j] )
{
foundMatch = true;
break;
}
if ( !foundMatch )
{
possibleMatches.push_back( ip );
qDebug() << "possible address: " << ifaces[i].humanReadableName() << "->" << ip;
}
}
}
}
return bReturn;
}
bool MainWindow::IsNetworkConnected()
{
bool bReturn = false;
QList<QString> possibleMatches;
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
if ( !ifaces.isEmpty() )
{
for(int i=0; i < ifaces.size(); i++)
{
unsigned int flags = ifaces[i].flags();
bool isLoopback = (bool)(flags & QNetworkInterface::IsLoopBack);
bool isP2P = (bool)(flags & QNetworkInterface::IsPointToPoint);
bool isRunning = (bool)(flags & QNetworkInterface::IsRunning);
bool isUp = (bool)(flags && QNetworkInterface::IsUp);
// If this interface isn't running, we don't care about it
if ( !isRunning )
continue;
// We only want valid interfaces that aren't loopback/virtual and not point to point
if ( !ifaces[i].isValid() || isLoopback || isP2P || !isUp)
continue;
QList<QHostAddress> addresses = ifaces[i].allAddresses();
for(int a=0; a < addresses.size(); a++)
{
// Ignore local host
if ( addresses[a] == QHostAddress::LocalHost ) continue;
// Ignore non-ipv4 addresses
if ( !addresses[a].toIPv4Address() )
continue;
QString ip = addresses[a].toString();
if ( ip.isEmpty() )
continue;
bool foundMatch = false;
for (int j=0; j < possibleMatches.size(); j++)
if ( ip == possibleMatches[j] )
{
foundMatch = true;
break;
}
if ( !foundMatch )
{
possibleMatches.push_back( ip );
qDebug() << "possible address: " << ifaces[i].humanReadableName() << "->" << ip;
}
}
}
}
return bReturn;
}
To copy to clipboard, switch view to plain text mode
This function always return "true" when i disconnected internet conection.
And my Network and Share Center:
Nework1.png
Please help me fix it.
Sorry my for my english skill.
Bookmarks