GetMacAddress


Description:
This function accepts a collection, and returns all available network card MAC addresses.
 
Code:
Option Explicit

Private Const NCBNAMSZ = 16
Private Const MAX_LANA = 254
Private Const NCBENUM = &H37
Private Const NCBRESET = &H32
Private Const NCBASTAT = &H33

Private Type ADAPTER_STATUS
    adapter_address(0 To 5) As Byte
    rev_major As Byte
    reserved0 As Byte
    adapter_type As Byte
    rev_minor As Byte
    duration As Integer
    frmr_recv As Integer
    frmr_xmit As Integer
    iframe_recv_err As Integer
    xmit_aborts As Integer
    xmit_success As Long
    recv_success As Long
    iframe_xmit_err As Integer
    recv_buff_unavail As Integer
    t1_timeouts As Integer
    ti_timeouts As Integer
    reserved1 As Long
    free_ncbs As Integer
    max_cfg_ncbs As Integer
    max_ncbs As Integer
    xmit_buf_unavail As Integer
    max_dgram_size As Integer
    pending_sess As Integer
    max_cfg_sess As Integer
    max_sess As Integer
    max_sess_pkt_size As Integer
    name_count As Integer
End Type

Private Type NAME_BUFFER
    name_(0 To NCBNAMSZ - 1) As Byte
    name_num As Byte
    name_flags As Byte
End Type
 
Private Type ASTAT
    adapt As ADAPTER_STATUS
    NameBuff(0 To 29) As NAME_BUFFER
End Type

Private Type NCB
    ncb_command As Byte
    ncb_retcode As Byte
    ncb_lsn As Byte
    ncb_num As Byte
    p_ncb_buffer As Long
    ncb_length As Integer
    ncb_callname(0 To NCBNAMSZ - 1) As Byte
    ncb_name(0 To NCBNAMSZ - 1) As Byte
    ncb_rto As Byte
    ncb_sto As Byte
    p_ncb_post As Long
    ncb_lana_num As Byte
    ncb_cmd_cplt As Byte
    ncb_reserve(0 To 10 - 1) As Byte
    ncb_event As Long
End Type
 
Private Type LANA_ENUM
    length As Byte
    lana(0 To MAX_LANA - 1) As Byte
End Type
 
Private Declare Function Netbios Lib "netapi32.dll" _
        (pncb As NCB) As Byte
Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (dest As Any, src As Any, ByVal length As Long) As Long

Public Sub GetMacAddress(colMacAddress As Collection)

    Dim Adapter As ASTAT
    Dim SNcb As NCB
    Dim uRetCode As Byte
    Dim NetName(0 To 49) As Byte
    Dim Blank(Len(SNcb)) As Byte
    Dim Blank2(Len(Adapter)) As Byte
    Dim lenum As LANA_ENUM
    Dim i As Long
    Dim j As Long
    Dim sMac As String

    CopyMemory SNcb, Blank(0), Len(SNcb)
    With SNcb
        .ncb_command = NCBENUM
        .p_ncb_buffer = VarPtr(lenum)
        .ncb_length = Len(lenum)
    End With
    uRetCode = Netbios(SNcb)
    
    For i = 0 To lenum.length - 1
        CopyMemory SNcb, Blank(0), Len(SNcb)
        With SNcb
            .ncb_command = NCBRESET
            .ncb_lana_num = lenum.lana(i)
        End With
    
        uRetCode = Netbios(SNcb)
        
        CopyMemory SNcb, Blank(0), Len(SNcb)
        CopyMemory Adapter, Blank2(0), Len(Adapter)
        With SNcb
            .ncb_command = NCBASTAT
            .ncb_lana_num = lenum.lana(i)
            .p_ncb_buffer = VarPtr(Adapter)
            .ncb_length = Len(Adapter)
            .ncb_callname(0) = Asc("*")
            For j = 1 To 15
                .ncb_callname(j) = Asc(" ")
            Next
        End With
        uRetCode = Netbios(SNcb)
        If uRetCode = 0 Then
            With Adapter.adapt
                sMac = Right("00" & Hex(.adapter_address(0)), 2) & _
                       Right("00" & Hex(.adapter_address(1)), 2) & _
                       Right("00" & Hex(.adapter_address(2)), 2) & _
                       Right("00" & Hex(.adapter_address(3)), 2) & _
                       Right("00" & Hex(.adapter_address(4)), 2) & _
                       Right("00" & Hex(.adapter_address(5)), 2)
            End With
            colMacAddress.Add sMac
        End If
    Next
End Sub
 
Sample Usage:
 
    Dim col As Collection
    Dim vItem As Variant
    
    Set col = New Collection
    
    GetMacAddress col
    
    For Each vItem In col
        Debug.Print vItem
    Next