Results 1 to 15 of 15

Thread: MAC Address is different in 2 places

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    MAC Address is different in 2 places

    I am concerned why I get 2 different MAC addresses. In my vb.net program:

    Code:
        Function getMacAddress()
            Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
            Return nics(1).GetPhysicalAddress.ToString
        End Function
    returns a value which does not match the value on the Network Connection Details popup form (the Physical Address field) of the computer. Can anyone shed some light as to why the MAC address coming out of the program is not the same as the Physical Address on the Network Connection Details popup?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MAC Address is different in 2 places

    You are almost certainly looking at different network interfaces if you see different MAC addresses.

    Firstly, exactly which "popup form" do you mean? What is the name of the network interface on that window and what is the Name property of the NetworkInterface that you're looking at in your code? I'm using Windows 10 and if I go to Settings > Network & Internet > Status and click on 'View your network properties', I get a window that lists network interfaces and includes a MAC address for each one. I just tried this code:
    vb.net Code:
    1. Imports System.Net.NetworkInformation
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim nics = NetworkInterface.GetAllNetworkInterfaces()
    7.  
    8.         For Each nic As NetworkInterface In nics
    9.             Console.WriteLine("{0}: {1}", nic.Name, nic.GetPhysicalAddress().ToString())
    10.         Next
    11.  
    12.         Console.ReadLine()
    13.     End Sub
    14.  
    15. End Module
    and I see very similar information with no discrepancies in the MAC addresses.

    I'm also interested to know why you're getting the second element from your array. What external network interfaces does you machine have? Does it have Ethernet and WiFi or just one and, if just one, which one? My guess would be that it has both and you're trying to get the MAC address for the WiFi interface. Is that correct?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    What I'm trying to get is the MAC address for the computer. The details popup is for the Local Area Connection Status. The Local Area Connection is the only connection on the Network Connections control panel that says it is connected.

    The computer is connected to a WiFi router which I have set so there's no actual WiFi. The WiFi router is necessary for my VOIP telephone. The computer also has an Ethernet cable connected to it for the internet.

    The code is something I copied that is basically Greek to me. There is also the following at the top of the program:

    Code:
    Imports System.Net.NetworkInformation

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MAC Address is different in 2 places

    Quote Originally Posted by projecttoday View Post
    What I'm trying to get is the MAC address for the computer.
    There's not really any such thing as a MAC address for a computer. MAC addresses apply to network interfaces. A computer with a single network interface is a rare thing these days, so pretty much all computers will have multiple MAC addresses these days.

    You should take a look at the documentation for the NetworkInterface class you're using. You should ALWAYS do that for any type that you don't fully understand the required use of. See what properties it has and what they mean. Then try something like the code I provided and perhaps look at ALL the property values. You can use the Name and Description to compare information found in the Control Panel and/or Settings app on Windows 10.

    Hard-coding an index like that is really not a good idea unless the app will run only on one machine that that machine configuration won't change. A better option is to determine which network interface is the right one based on its properties.

    If you're still confused, maybe it's time to provide some screenshots of your system configuration along with the output from code something like what I provided.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    I think I should re-phrase the question. I'm just trying to identify computers. This is for a password scheme. This code:

    Code:
       Function getMacAddress()
            Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
            Return nics(1).GetPhysicalAddress.ToString
        End Function
    works perfectly on my first test computer. I found this code by searching. I'm just concerned that it won't work on other computers. So I'm trying to understand better since I could test it on 10 computers and get lucky and the method could still be flawed. If this code will produce a reliable identifier on most computers, then I'm done!

    You have enlightened me into realizing that there can be multiple MAC addresses. Thank you for that. I suppose the next question is which one to use to identify a computer? Will the above code work on most computers? I realize that it might not be possible to come up with something that will work 100 per cent of the time.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: MAC Address is different in 2 places

    Well, it will return a consistent identifier on most computers, but almost by accident. You are getting the physical address of the second network interface in the collection (arrays are 0 based, remember, so index 1 is the second element of the array). If there is only one, then the code will straight up crash with an IndexOutOfRange exception, but as JMC stated, a computer with only one is a bit rare these days. However, it isn't that hard for a computer to change this. It's somewhat rare to do so, in most cases, since most people don't go tinkering with the hardware in their computer once it is running, but if they DO tinker with it, then you might still have that address at a different place in the array, or you might not have that address at all. And then there's MAC spoofing.

    So, it might work for a long time, it might work forever (if somebody spoofs the MAC address and just provides you with a dummy via software), and it might fail tomorrow. Totally reliable it is not, Whether or not it is sufficiently reliable is really up to you.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    The idea is that once a customer purchases the program, he/she will be able to use it on that computer as long as he/she is using that computer. So a configuration change on his/her end could thwart that. Rare, right? But still possible, making for an angry customer. I suppose I could just loop through the MAC addresses and check them all.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: MAC Address is different in 2 places

    Looping through the addresses would be better, but only very slightly. That would catch the case where the change re-ordered the mac addresses, but not catch the one where they switched to using a different NIC, which can result from a variety of things, these days, most of which I'm probably not aware of. The simplest example is that I just switched my computer for a new one. The old one is now my secondary system, so both still exist. That design would mean that I could use one or the other, but not both, and the primary computer would no longer be recognized, which would mean re-registering, I assume.

    Validating users is a sticky business. I'm not trying to be throwing bombs, and I don't have a good alternative. I'm just pointing out the potential issues, without providing any solutions.
    My usual boring signature: Nothing

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: MAC Address is different in 2 places

    Let me try to be excruciatingly detailed. I think JMC is right, and I think your code is written in a way that doesn't make it obvious.

    My work machine has a bewildering array of network connections available. In the "Network Connections" center I have 10 total connections available. Each of these has its own MAC address, so it's up to me to deduce which one is connected to the internet and use that. Right now, I can tell that happens to be a connection named "Ethernet". Now let's talk about your code.

    Your code says:
    Get all network interfaces, then return a String representing the MAC address of the 2nd one in that list.
    There's a lot of problems with that approach if you want a specific machine. One is the order of that list isn't guaranteed to match anything in your mind. Try this code out in a console application:
    Code:
    0) vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch)
    1) vEthernet (Realtek PCIe GBE Family Controller Virtual Switch)
    2) VirtualBox Host-Only Network
    3) VirtualBox Host-Only Network #2
    ...
    That's not the same order I see in Control Panel's default sort. In Control Panel, I see "Bluetooth Network Connection 2" as the first, then "Ethernet", then "Ethernet 2", etc. It turns out the one I want is (1) in the list, and it's in that spot in Control Panel, but it's a complete coincidence it sorted that way. Almost every other item in the list is in the "wrong" place when I compare the two.

    So if I want to get the MAC address of that one specifically, I need to know its name or some other way to identify it, then I need to make sure I get that specific one. This could do the trick:
    Code:
    Dim allInterfaces = NetworkInterface.GetAllNetworkInterfaces()
    For i As Integer = 0 To allInterfaces.Length - 1
        Dim thisInterface = allInterfaces(i)
        If thisInterface.Name.Contains("Realtek PCIe") Then
            Console.WriteLine(thisInterface.GetPhysicalAddress.ToString())
        End If
    Next
    But if you're wanting to run this on many computers, you're not going to be able to hard-code a name. It turns out finding the right one is pretty tricky unless the network configuration is very simple. The only useful property might be OperationalStatus, but it's possible for many interfaces to report "Up".

    So realistically, if you want the "right" MAC address, the best you might be able to do is present this list and choose one:
    Code:
    Dim allInterfaces = NetworkInterface.GetAllNetworkInterfaces()
    Dim candidates = allInterfaces.Where(Function(i) i.OperationalStatus = OperationalStatus.Up).ToArray()
    For i As Integer = 0 To candidates.Length - 1
        Dim candidate = candidates(i)
        Console.WriteLine("{0}) {1} -> {2}", i, candidate.Name, candidate.GetPhysicalAddress.ToString())
    Next
    That's really the best you can do. If this tool is only ever intended to run on your personal machine, maybe you can hard-code something better. But answering the general question, "What is my MAC address?" is not easy given the tools at hand, and it can be an ambiguous question.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    Okay, Shaggy. No pain, no gain, I guess.

    In determining whether or not to go to the trouble of changing the code to make it "only very slightly" better, how likely is it that someone would make a change that would re-order mac addresses?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    Okay, Sitten, so you're saying loop through the nics filtering out those which are operationally down?

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: MAC Address is different in 2 places

    That will get you a shorter list of potential candidates. If only one is left, then that HAS to be the right one. But there's a lot of ways to have at least two operational network interfaces on modern machines.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: MAC Address is different in 2 places

    Quote Originally Posted by Sitten Spynne View Post
    But there's a lot of ways to have at least two operational network interfaces on modern machines.
    Indeed. I built a new system a few months ago and the motherboard has Killer Double Shot Pro, which means that it has two wired interfaces and one wireless interface that can all be simultaneously connected. A great many laptops these days have a wired and a wireless connection too, although they are less likely to be simultaneously active. Bluetooth radios get a MAC address too though, so they will often be active at the same time as Ethernet or WiFi connections.
    Last edited by jmcilhinney; May 13th, 2017 at 07:46 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: MAC Address is different in 2 places

    Quote Originally Posted by projecttoday View Post
    The idea is that once a customer purchases the program, he/she will be able to use it on that computer as long as he/she is using that computer. So a configuration change on his/her end could thwart that. Rare, right? But still possible, making for an angry customer. I suppose I could just loop through the MAC addresses and check them all.
    So you have identified issues with half the problem, the ID of the machine. What about how you will manage your customers once you have that worked out?

    Based on the number of copies sold a machine type ID might be a good way to go. However, the rare event i.e. new NIC, on one machine might not be so rare on thousands of machines.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: MAC Address is different in 2 places

    I see what you mean. So I think I'll have to change it so that, once verified, a given copy will always work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width