How to reliably and quickly get the MAC address of a network card given its device instance ID

前端 未结 3 999
感动是毒
感动是毒 2021-02-08 04:19

Given a device instance ID for a network card, I would like to know its MAC address. Example device instance ID on my system for integrated Intel Gigabit card:

         


        
3条回答
  •  难免孤独
    2021-02-08 04:54

    Here's one way to do it:

    1. Call GetAdaptersAddresses to get a list of IP_ADAPTER_ADDRESSES structs
    2. Iterate over each adapter and get its GUID from the AdapterName field (I'm not sure if this behaviour is guaranteed, but all the adapters in my system have a GUID here, and the documentation says the AdapterName is permanent)
    3. For each adapter read the registry key from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\\Connection\PnPInstanceID (if it exists) (got this idea from here; searching on Google that key seems to be well documented, so it's not likely to change)
    4. From this key you get the device ID for the adapter (something like: PCI\VEN_14E4&DEV_16B1&SUBSYS_96B11849&REV_10\4&2B8260C3&0&00E4)
    5. Do this for each adapter until you find a match. When you get your match just go back to the IP_ADAPTER_ADDRESSES and look at the PhysicalAddress field
    6. Get a beer (optional)

    It wouldn't be Windows if there weren't a million ways to do something!

提交回复
热议问题