PDA

View Full Version : filter QNetworkInterface::hardwareAddress()



prophet0
11th April 2012, 08:23
The following code



foreach (const QNetworkInterface &ni, QNetworkInterface::allInterfaces()) {
if (!(ni.flags() & ni.IsLoopBack)){
macName = ni.hardwareAddress(); // lets match & get our information for this device
qDebug() << macName;


outputs



"00:1C:DF:93:1D:5D"
"00:1C:DF:93:1D:5D"
"00:23:AE:A8:26:FC" // this one being Local Area Connection
"00:50:56:C0:00:01"
"00:50:56:C0:00:08"
"3E:9B:20:52:41:53"
"3E:9B:20:52:41:53"
"00:23:AE:A8:26:FC"
"00:23:AE:A8:26:FC"
"3E:9B:20:52:41:53"
"3E:9B:20:52:41:53"
"3E:9B:20:52:41:53"
"3E:9B:20:52:41:53"
"20:41:53:59:4E:FF"
"00:00:00:00:00:00:00:00"
"00:00:00:00:00:00:00:E0"
"00:00:00:00:00:00:00:E0"
"00:00:00:00:00:00:00:E0"
"00:00:00:00:00:00:00:E0"
"00:00:00:00:00:00:00:E0"


how can i filter threw the hardware addresses where QNetworkInterface::humanReadableName = local area connection and spit that one out only ?

Spitfire
11th April 2012, 17:12
Unless I'm not getting something it sounds pretty straight forward.

Just loop through the interfaces comparing humanReadableName() to your string and if you find a match return hardwareAddress().

ChrisW67
12th April 2012, 00:47
If you don't want all the addresses why go looking for them? QNetworkInterface::interfaceFromName() is the direct method.

prophet0
12th April 2012, 08:51
QString string = "Local Area Connection";
qDebug() << ni.interfaceFromName(string);


shows



QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )

QNetworkInterface(name = "", hardware address = "", flags = , entries = () )



So now you can see my confusion

Spitfire
12th April 2012, 10:53
interfaceFromName() expects interface name returned by QNetworkInterface::name() (ie "{D6637618-7E2B-466B-B02B-C35A1FA9118F}") not QNetworkInterface::humanReadableName().

So, if you want to find interface by human readable name you have to loop through the interfaces and find it yourself.

In the end it's only few lines of code:


QList< QNetworkInterface > it = QNetworkInterface::allInterfaces();
for( int i = 0; i < it.size(); ++i )
{
if( it.at( i ).humanReadableName() == "Local Area Connection" )
{
qDebug() << it.at( i ).hardwareAddress();
break;
}
}

prophet0
12th April 2012, 10:59
That works perfectly thanks very much

ChrisW67
12th April 2012, 11:52
interfaceFromName() expects interface name returned by QNetworkInterface::name() (ie "{D6637618-7E2B-466B-B02B-C35A1FA9118F}") not QNetworkInterface::humanReadableName().
Is that some Windows perversion? QNetworkInterface::interfaceFromName() works perfectly fine with "eth0", i.e. the same as the humanReadableName(), on Linux.

Spitfire
12th April 2012, 13:34
On linux both are the same but Microsoft invented 'better' way, obviously :)