Link to home
Start Free TrialLog in
Avatar of xassets
xassets

asked on

MAC Address Code for Wireless LAN (WLAN) Computers

Hi There.

I have the following code off some web site to get mac addresses but it seems to return zeroes for wireless LAN computers. Anyone know how to get the mac address for wlan computers? ipconfig /all seems to do it but we'd want some kind of API call to get it.

Thanks


Option Explicit
Public Const NCBASTAT As Long = &H33
Public Const NCBNAMSZ As Long = 16
Public Const HEAP_ZERO_MEMORY As Long = &H8
Public Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
Public Const NCBRESET As Long = &H32

Public Type NET_CONTROL_BLOCK  'NCB
   xy_command    As Byte
   xy_retcode    As Byte
   xy_lsn        As Byte
   xy_num        As Byte
   xy_buffer     As Long
   xy_length     As Integer
   xy_callname   As String * NCBNAMSZ
   xy_name       As String * NCBNAMSZ
   xy_rto        As Byte
   xy_sto        As Byte
   xy_post       As Long
   xy_lana_num   As Byte
   xy_cmd_cplt   As Byte
   xy_reserve(9) As Byte 'Reserved, must be 0
   xy_event      As Long
End Type

Public Type ADAPTER_STATUS
   adapter_address(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
   mx_cfg_ncbs      As Integer
   mx_ncbs          As Integer
   xmit_buf_unavail  As Integer
   mx_dgram_size    As Integer
   pending_sess      As Integer
   mx_cfg_sess      As Integer
   mx_sess          As Integer
   mx_sess_pkt_size As Integer
   name_count        As Integer
End Type
   
Public Type NAME_BUFFER
   name        As String * NCBNAMSZ
   name_num    As Integer
   name_flags  As Integer
End Type

Public Type ASTAT
   adapt          As ADAPTER_STATUS
   NameBuff(30)   As NAME_BUFFER
End Type

Public Declare Function Netbios Lib "netapi32" _
   (pncb As NET_CONTROL_BLOCK) As Byte
     
Public Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (hpvDest As Any, ByVal _
   hpvSource As Long, ByVal _
   cbCopy As Long)
     
Public Declare Function GetProcessHeap Lib "kernel32" () As Long

Public Declare Function HeapAlloc Lib "kernel32" _
  (ByVal hHeap As Long, _
   ByVal dwFlags As Long, _
   ByVal dwBytes As Long) As Long
     
Public Declare Function HeapFree Lib "kernel32" _
  (ByVal hHeap As Long, _
   ByVal dwFlags As Long, _
   lpMem As Any) As Long


Public Function GetMACAddress(lLana As Long) As String

  'retrieve the MAC Address for the network controller
  'installed, returning a formatted string
   
   Dim tmp As String
   Dim pASTAT As Long
   Dim NCB As NET_CONTROL_BLOCK
   Dim AST As ASTAT

  'The IBM NetBIOS 3.0 specifications defines four basic
  'NetBIOS environments under the NCBRESET command. Win32
  'follows the OS/2 Dynamic Link Routine (DLR) environment.
  'This means that the first NCB issued by an application
  'must be a NCBRESET, with the exception of NCBENUM.
  'The Windows NT implementation differs from the IBM
  'NetBIOS 3.0 specifications in the xy_CALLNAME field.
   NCB.xy_command = NCBRESET
   Call Netbios(NCB)
   
  'To get the Media Access Control (MAC) address for an
  'ethernet adapter programmatically, use the Netbios()
  'NCBASTAT command and provide a "*" as the name in the
  'NCB.xy_CallName field (in a 16-chr string).
   NCB.xy_callname = "*               "
   NCB.xy_command = NCBASTAT
   
  'For machines with multiple network adapters you need to
  'enumerate the LANA numbers and perform the NCBASTAT
  'command on each. Even when you have a single network
  'adapter, it is a good idea to enumerate valid LANA numbers
  'first and perform the NCBASTAT on one of the valid LANA
  'numbers. It is considered bad programming to hardcode the
  'LANA number to 0 (see the comments section below).
   NCB.xy_lana_num = lLana
   NCB.xy_length = Len(AST)
   
   pASTAT = HeapAlloc(GetProcessHeap(), _
                      HEAP_GENERATE_EXCEPTIONS Or _
                      HEAP_ZERO_MEMORY, _
                      NCB.xy_length)
           
   If 0 = pASTAT Then
        MsgBox "PASTAT WAS ZERO - UNABLE TO ALLOCATE HEAP MEMORY"
      Err.Raise 32001, , "Memory Allocation failed!"
      Exit Function
   End If
   
   NCB.xy_buffer = pASTAT
   Call Netbios(NCB)
   
   CopyMemory AST, NCB.xy_buffer, Len(AST)
   
    MsgBox "RAW ADDRESSES : " & _
         AST.adapt.adapter_address(0) & " " & _
         AST.adapt.adapter_address(1) & " " & _
         AST.adapt.adapter_address(2) & " " & _
         AST.adapt.adapter_address(3) & " " & _
         AST.adapt.adapter_address(4) & " " & _
         AST.adapt.adapter_address(5)
   
   tmp = Right$("00" & Hex(AST.adapt.adapter_address(0)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(1)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(2)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(3)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(4)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(5)), 2)
                   
   HeapFree GetProcessHeap(), 0, pASTAT
   
   GetMACAddress = tmp

End Function






ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xassets
xassets

ASKER

got a solution within 30 mins. Well done.