Solved

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

Posted on 2014-02-19
8
203 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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