locked
MAC address in Windows 7

    Question

  • Please help me in these two issues
     
    1. Below code shows mac address in XP but empty string in Windows7.
    2. Is there any chance to find mac address directly with a query instead of looping "foreach" statement.

     Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim mac As String = ""
            Dim moc As ManagementObjectCollection = mc.GetInstances
    
            For Each mo As ManagementObject In moc
                If mo.Item("IPEnabled") Then
                    mac = mo.Item("MacAddress").ToString
                End If
            Next
    
    
    
    i got the mac address when the cable is plugged, but when i unplugged it got the same error(empty string). Most of my clients are not having net connection.

     is there any alternate way to get mac address in win 7?

     

    Best Regards,

    Saturday, October 08, 2011 3:02 AM

Answers

  • I've got 6 addresses.

    ' Add a reference to and import System.Net
    For Each ni In NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
      Console.WriteLine(ni.Name & " " & ni.GetPhysicalAddress.ToString)
    Next
    Console.ReadKey()

    Saturday, October 08, 2011 8:00 AM
  • yes of course

    Imports System.Management
    Module Module1
        Sub Main()
            Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim mac As String = ""
            Dim moc As ManagementObjectCollection = mc.GetInstances
            For Each mo As ManagementObject In moc
                Console.WriteLine(mo.Item("MacAddress"))
            Next
            Console.ReadKey()
        End Sub
    End Module
    
    



    Success
    Cor
    Saturday, October 08, 2011 5:34 PM
  • Using the following code I was able to determine what the mac address of my disabled wired(Ethernet) connection was.  In my case it showed up as a WAN port...  Try a new form with two buttons.

     

    Imports System
    Imports System.Management 'add this as a reference also
    Imports System.Windows.Forms

    Public Class Form1

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Debug.WriteLine("")
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration")

                Debug.WriteLine("-----------------------------------")
                Debug.WriteLine("Win32_NetworkAdapterConfiguration")
                Debug.WriteLine("-----------------------------------")

                For Each queryObj As ManagementObject In searcher.Get()
                    Debug.WriteLine("")
                    Debug.WriteLine("Caption: {0}", queryObj("Caption"))
                    Debug.WriteLine("Description: {0}", queryObj("Description"))
                    Debug.WriteLine("InterfaceIndex: {0}", queryObj("InterfaceIndex"))
                    Debug.WriteLine("MACAddress: {0}", queryObj("MACAddress"))
                Next
            Catch err As ManagementException
                'MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Debug.WriteLine("")
            Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapter")

                Debug.WriteLine("-----------------------------------")
                Debug.WriteLine("Win32_NetworkAdapter")
                Debug.WriteLine("-----------------------------------")

                For Each queryObj As ManagementObject In searcher.Get()
                    Debug.WriteLine("")
                    Debug.WriteLine("AdapterType: {0}", queryObj("AdapterType"))
                    Debug.WriteLine("Index: {0}", queryObj("Index"))
                Next
            Catch err As ManagementException
                'MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub
    End Class

    Used WMI Code Creator to figure this out.


    Serial Port      Random      Microsoft® Community Contributor 2011

    Sunday, October 09, 2011 2:31 PM

All replies

  • I've got 6 addresses.

    ' Add a reference to and import System.Net
    For Each ni In NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
      Console.WriteLine(ni.Name & " " & ni.GetPhysicalAddress.ToString)
    Next
    Console.ReadKey()

    Saturday, October 08, 2011 8:00 AM
  • i got the mac address when the cable is plugged, but when i unplugged it got the same error(empty string). Most of my clients are not having net connection.
    Saturday, October 08, 2011 8:05 AM
  • Saturday, October 08, 2011 12:39 PM
  • This code returns one mac address on my computer (which I don't use).

     

    Imports System.Management
    Module Module1
        Sub Main()
            Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim mac As String = ""
            Dim moc As ManagementObjectCollection = mc.GetInstances
            For Each mo As ManagementObject In moc
                If CBool(mo.Item("IPEnabled")) Then
                    Console.WriteLine(mo.Item("MacAddress"))
                End If
            Next
            Console.ReadKey()
        End Sub
    End Module
    

    Put Option Strict on in top of your code

     


    Success
    Cor

    Saturday, October 08, 2011 2:00 PM
  • MacAdress "00:00 ...." turning off the internet
    Saturday, October 08, 2011 4:32 PM
  • yes of course

    Imports System.Management
    Module Module1
        Sub Main()
            Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim mac As String = ""
            Dim moc As ManagementObjectCollection = mc.GetInstances
            For Each mo As ManagementObject In moc
                Console.WriteLine(mo.Item("MacAddress"))
            Next
            Console.ReadKey()
        End Sub
    End Module
    
    



    Success
    Cor
    Saturday, October 08, 2011 5:34 PM
  • not working on the Win7
    Sunday, October 09, 2011 9:27 AM
  • With what kind of OS do you think I've tested it?

     


    Success
    Cor
    Sunday, October 09, 2011 10:09 AM
  • Using the following code I was able to determine what the mac address of my disabled wired(Ethernet) connection was.  In my case it showed up as a WAN port...  Try a new form with two buttons.

     

    Imports System
    Imports System.Management 'add this as a reference also
    Imports System.Windows.Forms

    Public Class Form1

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                Debug.WriteLine("")
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration")

                Debug.WriteLine("-----------------------------------")
                Debug.WriteLine("Win32_NetworkAdapterConfiguration")
                Debug.WriteLine("-----------------------------------")

                For Each queryObj As ManagementObject In searcher.Get()
                    Debug.WriteLine("")
                    Debug.WriteLine("Caption: {0}", queryObj("Caption"))
                    Debug.WriteLine("Description: {0}", queryObj("Description"))
                    Debug.WriteLine("InterfaceIndex: {0}", queryObj("InterfaceIndex"))
                    Debug.WriteLine("MACAddress: {0}", queryObj("MACAddress"))
                Next
            Catch err As ManagementException
                'MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Debug.WriteLine("")
            Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapter")

                Debug.WriteLine("-----------------------------------")
                Debug.WriteLine("Win32_NetworkAdapter")
                Debug.WriteLine("-----------------------------------")

                For Each queryObj As ManagementObject In searcher.Get()
                    Debug.WriteLine("")
                    Debug.WriteLine("AdapterType: {0}", queryObj("AdapterType"))
                    Debug.WriteLine("Index: {0}", queryObj("Index"))
                Next
            Catch err As ManagementException
                'MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub
    End Class

    Used WMI Code Creator to figure this out.


    Serial Port      Random      Microsoft® Community Contributor 2011

    Sunday, October 09, 2011 2:31 PM