I am have different network adapters plugged to my PC. I get the MAC address for each one of them using GetAdaptersInfo(). How can I get the Mac address of the network adapter through which I am connected to the Internet? So basically I need to know through which adapter I'm connected to the Internet and then get the Mac address of that adapter. How do we do that?
Posted on 2009-05-10 14:21:55 by XCHG
You are connected through both adapters (from software's point of view). It's just that you receive some packet via one of them, and some packets by the other. The fact that you're physically connected with only one adapter is just a special case when you don't recevie packet via the other adapter. I think the most reliable method is to connect to some remote address which will send you its response via the adapter you want it to and then make it tell you your IP from its point of view. There may be some easier ways but I don't believe they are reliable with all network configurations.  ...Or maybe I'm wrong, who knows ;p
Posted on 2009-05-10 15:52:55 by ti_mo_n
Thanks ti_mo_n. Say I have a bluetooth adapter and a network adapter. I plug my network cable to my network adapter although I am also able to connect to Internet through my bluetooth dongle. Both these devices have MAC addresses. Say I want to get the MAC address of the device that is currently connected to the Internet. How do I do that? The solution that comes to my mind is to get my current IP address using Win32 API, then go through the network adapters using GetAdaptersInfo() and compare the IP address with the IP addresses of the network adapters. Whichever matches, that's the device that is connected to the Internet. There I can get the MAC address of the device and BAAAAAAAM. Hm?
Posted on 2009-05-10 16:29:46 by XCHG
Windows first tries to connect via the adapter which is first on the list (has higher priority. see: control panel -> network connections -> Advnaced (menu) -> advanced settings. The first tab show the order in which the adapters are used to connect. At first, Windows tries to use the first one. If it fails, it tries the second one, etc. If all fail then Windows tells you that it failed to connect. So you either ask Windows itself which adapter did it use to connect (I don't know if/how it can be done), or -as you suggested- connect to remote host, check the socket's local IP and then compare it with adapters' IPs. But I don't know how reliable is that. Especially when you have software nerwork bridges installed.
Posted on 2009-05-10 22:18:44 by ti_mo_n