Solved

VB.net Reliable way to always return the same code for the MACAddress

Posted on 2014-02-19
8
206 Views
Last Modified: 2014-02-27
Hi

The following function always returned my machine's MAC addresss but suddenly returns a blank string. Why might the cause be?

    Friend Function GetMACAddress() As String

        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim MACAddress As String = String.Empty
        For Each mo As ManagementObject In moc

            If (MACAddress.Equals(String.Empty)) Then
                If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()

                mo.Dispose()
            End If
            MACAddress = MACAddress.Replace(":", String.Empty)

        Next
        Return MACAddress
    End Function

Open in new window


The function below seems to work better but it said that that  only refers to the first network adapter
    Function getMacAddress()
        Dim nics() As NetworkInterface = _
              NetworkInterface.GetAllNetworkInterfaces
        Return nics(0).GetPhysicalAddress.ToString
    End Function

Open in new window



What is a reliable way to always return the same code for the MACAddress
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39870171
Problem with your first function is that it loops through the network adaptors and returns you MAC of the last adaptor in the loop. Problem with second function is that it always returns MAC of the first adaptor. What is your requirement? Always return the same MAC? Or always return some MAC?
0
 

Author Comment

by:murbro
ID: 39870283
Hi
It is to always return the same MAC
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39870410
I don't know the context of your program but may be you run this function to get a valid MAC once and then store the MAC somewhere instead of calling this function every time.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:murbro
ID: 39879048
Hi
I am using the MAC address as a way to identify a valid user so it is stored in my cloud SQL
database. Each time the user connects my code validates that the MAC address is the same. So I am just looking for a way to return the same MAC address
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39885332
One option then is to get MAC addresses of all the physical NICs in user's computer and then if any of the MACs matches with the one that you have in your database then its a known user.
0
 

Author Closing Comment

by:murbro
ID: 39893892
thanks very much
0
 

Author Comment

by:murbro
ID: 39893898
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question