retrieve MAC address from ethernet card using VB 6

Posted on 2009-02-14
Last Modified: 2012-05-06
Is there a way to retrieve a MAC address on a PC using VB 6? A sample of code would be great.

Thanks in advance.
Question by:samic400
    LVL 13

    Expert Comment

    One simple solution is to use the following from a  shell
    This will write all the info in the text file then you can read and parse the text file and get the physical address.

    Direct from source code
    LVL 6

    Expert Comment


    Private Sub Form_Load()
        Dim MACs() As String
        Dim i As Long
        MACs = MACAddressWMI
        For i = 0 To UBound(MACs)
            If Len(MACs(i)) > 0 Then List1.AddItem MACs(i)
        Next i
    End Sub
    Public Function MACAddressWMI() As String()
        Dim WMIobj As Object
        Dim MACobj As Object
        Dim s() As String
        On Error Goto ErrOut
        ReDim s(0)
        Set WMIobj = GetObject("winmgmts:").ExecQuery("SELECT MACAddress FROM Win32_NetworkAdapter " & _
                  "WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))")
        For Each MACobj In WMIobj
            ReDim s(UBound(s) + 1)
            s(UBound(s)) = MACobj.MACAddress
        Next MACobj
        MACAddressWMI = s
        Exit Function
        MsgBox "Error! WMI not found!"
    End Function

    Open in new window

    LVL 13

    Author Comment

    VK, this would retrieve a MAC address for a wireless router card as well?
    LVL 6

    Expert Comment

    It doesn't make a difference if the network atdapter is wireless or not.
    It has to be a built in adapter.
    LVL 13

    Author Comment

    The problem with this code is when I run this from my laptop, it come up with an error "WMI Not Found" and it cannot find any MAC address at all. When I run it from my desktop, I get an entry in the listbox.
    LVL 6

    Assisted Solution

    LVL 2

    Accepted Solution

    LVL 13

    Author Closing Comment

    Team2005, this worked great, VK I still couldn't get the MAC address to appear in any of the examples. Thanks for the help folks!
    LVL 2

    Expert Comment


    Thanks for the points :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    VBA Code Error 3 54
    Hidden and protected sheets 12 48
    VBA to create a graph with dynamic data in a table 8 51
    Access 2016 VB code 9 69
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now