Announcement

Collapse
No announcement yet.

How to get the MAC Adress(es)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get the MAC Adress(es)

    Hi,

    not really an internet question, but I want to read the Network Card Address (MAC) of the networkcards (could be more) ... could someone help me ?
    Regards,
    Hubert

    ------------------------------------
    http://familie-brandel.de/index_e.html

  • #2
    MAC Address

    Well since there is little information as to how you want to do it you can start with these:

    From command shell:
    ipconfig /all

    netsh interface ip show interface

    You can also use WMI to query network information.
    Warped by the rain, Driven by the snow...

    jimatluv2rescue.com

    Comment


    • #3
      Here are links to a couple of code samples that use the Ip Helper Functions to retrieve network info..

      John McWilliams http://www.powerbasic.com/support/pb...07&postcount=1

      Pierre Bellisle http://www.powerbasic.com/support/pb...07&postcount=4
      Rgds, Dave

      Comment


      • #4
        And if you want the MAC address of another computer on your LAN:

        Code:
        'Function to find the NIC MAC address of a remote computer.
           
        'Need Windows 2000 or later
        'Windows 9x/Me unsupported
           
        'PB/Win 7.00
        'WinApi 2002/10/30
           
        
        #Include "COMMCTRL.INC"
        #Include "WIN32API.INC"
        #Include "WSOCK32.INC"
        #Include "IPHLPAPI.INC"
           
        %IPBox        = 1001
        %LabelIP      = 1002
        %LabelMac     = 1003
        %TextBox      = 1004
        %ButtonGetMac = 1005
          
        Global hDlg As Long
        '______________________________________________________________________________
         
        Function GetRemoteMACAddress(sRemoteIP As String) As String
         Local dwRemoteIP        As Dword
         Local zRemoteIP         As Asciiz * 16
         Local dwMacAddrLen      As Dword
         Local sMacByte          As String
         Local sRemoteMacAddress As String
         Local lCounter          As Long
         Dim   MacArray(0 To 1)  As Dword
          
         zRemoteIP  = sRemoteIP            'Copy string in asciiz.
         dwRemoteIP = inet_addr(zRemoteIP) 'Convert Asciiz IP to DWord IP.
         If dwRemoteIP <> 0 Then
           dwMacAddrLen = 6
           If SendARP(dwRemoteIP, 0&, MacArray(0), dwMacAddrLen) = %NO_ERROR Then
             If IsTrue(MacArray(0) + MacArray(1)) And dwMacAddrLen = 6 Then  'Check for valid result.
               For lCounter = 0 To 5   'Build string in a "00-00-00-00-00-00" format.
                 sMacByte = Hex$(Peek(VarPtr(MacArray(0)) + lCOunter), 2)
                 sRemoteMacAddress = sRemoteMacAddress & sMacByte
                 If lCounter < 5 Then sRemoteMacAddress = sRemoteMacAddress & "-"
               Next
               Function = sRemoteMacAddress
             End If
           End If
         End If
          
        End Function  
        '______________________________________________________________________________
          
        CallBack Function DlgProc() As Long
         Local sRemoteIP  As String
         Local sRemoteMac As String
          
         Select Case CbMsg
           Case %WM_INITDIALOG
          
           Case %WM_COMMAND
             Select Case CbCtl
          
                Case %IDCANCEL
                  If CbCtlMsg = %BN_CLICKED Then Dialog End CbHndl, 0
          
                Case %ButtonGetMac, %IDOK
                  Control Get Text hDLG, %IPBox To sRemoteIP
                  sRemoteMac = GetRemoteMACAddress(sRemoteIP)
                  If Len(sRemoteMac) Then
                    Control Set Text hDLG, %TextBox, sRemoteMac
                  Else
                    Control Set Text hDLG, %TextBox, "MAC not found!"
                  End If
             End Select
          
         End Select
          
        End Function
        '______________________________________________________________________________
          
        Function WinMain (ByVal hInst As Long, ByVal hPrevInstance As Long, _
                          lpszCmdLine As Asciiz Ptr, ByVal nCmdShow As Long) As Long
          
          Dialog New 0, "Get remote MAC address", , , 195, 102, _
                      %WS_POPUP Or %WS_VISIBLE Or %WS_CLIPSIBLINGS Or %WS_CAPTION Or _
                      %WS_SYSMENU Or %WS_THICKFRAME Or %WS_MINIMIZEBOX Or %WS_MAXIMIZEBOX Or _
                      %DS_3DLOOK Or %DS_NOFAILCREATE Or %DS_SETFONT, _
                      %WS_EX_CONTROLPARENT Or %WS_EX_WINDOWEDGE To hDlg
          
          Dialog Send hDlg, %WM_SETICON, %ICON_SMALL, LoadIcon(%Null, ByVal %IDI_APPLICATION)
          Dialog Send hDlg, %WM_SETICON, %ICON_BIG,   LoadIcon(%Null, ByVal %IDI_APPLICATION)
          
          InitCommonControls
          
          Control Add "SysIPAddress32", hDlg, %IPBox, "", 15, 27, 108, 12, _
                      %WS_CHILD Or %WS_VISIBLE Or %WS_TABSTOP, _
                      %WS_EX_CLIENTEDGE
          
          Control Add Label, hDlg, %LabelIP, "Remote IP address", 15, 9, 108, 12, _
                      %WS_CHILD Or %WS_VISIBLE
          
          Control Add Label, hDlg, %LabelMac, "Remote MAC address", 12, 54, 99, 12, _
                      %WS_CHILD Or %WS_VISIBLE
          
          Control Add TextBox, hDlg, %TextBox, "00-00-00-00-00-00", 12, 69, 144, 15, _
                      %WS_CHILD Or %WS_VISIBLE Or %WS_TABSTOP Or %ES_AUTOHSCROLL, _
                      %WS_EX_CLIENTEDGE
          
          Control Add Button, hDlg, %ButtonGetMac, "Get MAC", 141, 27, 39, 18, _
                      %WS_CHILD Or %WS_VISIBLE Or %WS_TABSTOP Or %BS_CENTER Or _
                      %BS_VCENTER
          
          Dialog Show Modal hDlg, Call DlgProc
          
        End Function
        '______________________________________________________________________________
        Software makes Hardware Happen

        Comment


        • #5
          Hi,

          thanks to all.

          I knew the "IPCONFIG /ALL" thing, and now I will put it into the code directly
          Regards,
          Hubert

          ------------------------------------
          http://familie-brandel.de/index_e.html

          Comment


          • #6
            Very simple way can be found at

            http://www.powerbasic.com/support/pb...hlight=sendarp

            Comment


            • #7
              Hi,

              this is a pretty code thanks.
              I am wondering why my search on MAC* did not find it ...
              I am still bad in searching inside this forum :-( ... I remember, there was too many hits ...
              Last edited by Hubert Brandel; 19 Feb 2009, 03:04 AM.
              Regards,
              Hubert

              ------------------------------------
              http://familie-brandel.de/index_e.html

              Comment


              • #8
                Originally posted by Hubert Brandel View Post
                Hi,

                this is a pretty code thanks.
                I am wondering why my search on MAC* did not find it ...
                I am still bad in searching inside this forum :-( ... I remember, there was too many hits ...
                Joe's code and mine pretty much have the same guts. His is a bit more polished than mine.... better error checking and a nicer GUI. Mine was just an example to show the SendARP API usage.

                What's nice is that both code segments were written *years* ago! Hopefully one of our examples made it into someones library and serve to push someone else along to explore the API.

                Comment


                • #9
                  below is the code i use to get mac (see comment notes for origin). haven't used it in a while, but it worked ok as i remember:
                  Code:
                  '
                  '  pb_mac.inc
                  '
                  '  Functions to return the mac address of a network card.
                  '  It calls dynamically (call dword) in case netapi32.dll isn't
                  '  installed on the system.
                  '
                  '  VB code posted by Dave Navarro
                  '  Original author unknown
                  '  Translated from VB to PB by Don Dickinson
                  '
                  '  Functions exported:
                  '     initNetBios
                  '     freeNetBios
                  '     pbNetBios      - note:  don't try to call this NetBios as that
                  '                             call is already defined in win32api.inc.
                  '                             our definition here is different.
                  '     GetMacAdd
                  '
                  '  Dependencies
                  '     This module depends on win32api.inc (not included here)
                  '
                  #If Not %Def(%PB_MAC_INC)
                  %PB_MAC_INC = 1
                  
                  '
                  '  Constants
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  %NCBENUM = &H37      'enumerate lana numbers
                  %NCBRESET = &H32     'reset
                  %NCBASTAT = &H33     'adapter status
                  
                  '
                  '  Structures
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  '
                  '- Network Control Block
                  Type NTWRKCNTRLBLCK
                     ncb_command As Byte              'command code
                     ncb_retcode As Byte              'return code
                     ncb_lsn As Byte                  'local session number
                     ncb_num As Byte                  'number of our network name
                     ncb_buffer As Long               'address of message buffer
                     ncb_length As Integer            'size of message buffer
                     ncb_callname(0 To 15) As Byte    'blank-padded name of remote
                     ncb_name(0 To 15) As Byte        'our blank-padded netname
                     ncb_rto As Byte                  'rcv timeout/retry count
                     ncb_sto As Byte                  'send timeout/sys timeout
                     lpFunc As Long                   'POST routine address
                     ncb_lana_num As Byte             'lana (adapter) number
                     ncb_cmd_cplt As Byte             '0xff => command pending
                     ncb_reserve(0 To 9) As Byte      'reserved, used by BIOS
                     ncb_event As Long                'HANDLE to Win32 event which will be set to the signalled
                  End Type                            'state when an ASYNCH command completes
                  Type LANA_ENUM
                     length As Byte
                     lana(0 To 256) As Byte
                  End Type
                  
                  '- Structure returned to the NCB command NCBSTAT is ADAPTER_STATUS
                  '  followed by an array of NAME_BUFFER structures => NET_STATUS
                  '
                  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
                  
                  Type NAME_BUFFER
                     name_(0 To 15) As Byte
                     name_num As Byte
                     name_flags As Byte
                  End Type
                  
                  Type NET_STATUS
                     Adapter As ADAPTER_STATUS
                     NameBuffer(30) As NAME_BUFFER
                  End Type
                  
                  '
                  '  Call DWORD template
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Declare Function callTemplate_NetBios(pncb As NTWRKCNTRLBLCK) As Byte
                  
                  
                  '
                  '  Global Variables
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Global g_Lib As Long
                  
                  '
                  '  initNetBios
                  '  This loads the netbios dll into memory. You don't have to call this function,
                  '  but it may speed up your program if you have to make many netbios calls
                  '  as it loads the dll into memory so it doesn't get loaded and unloaded
                  '  on each call to NetBios.
                  '
                  '  IMPORTANT: If you Call this function, you must call freeNetBios before
                  '             your program ends to unload the netapi32.dll library.
                  '
                  '  RETURNS: %True if successful, %False if not.                        EXPORTED
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Function initNetBios Alias "initNetBios" Export As Long
                  
                     If g_Lib = 0 Then
                        g_Lib = LoadLibrary("netapi32.dll")
                        If g_Lib Then
                           Function = %True
                        Else
                           Function = %False
                        End If
                     Else
                  
                        '- It's already initialized
                        Function = %True
                     End If
                  
                  End Function
                  
                  '
                  '  freeNetBios
                  '  This function unloads the netbios dll. If you call initNetBios, you
                  '  must call this before your program ends.                            EXPORTED
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Sub freeNetBios Alias "freeNetBios" Export
                     If g_Lib Then
                        FreeLibrary g_Lib
                        g_Lib = 0
                     End If
                  End Sub
                  
                  '
                  '  pbNetBios
                  '  Dynamically load the netapi32.dll and CALL DWORD wrapper for the
                  '  NetBios command exported from it.
                  '  Returns %True if the library could be loaded. The returned byte from
                  '  the NetBios api call is returned in the bReturn parameter.
                  '  Returns %False if netapi32.dll couldn't be loaded or the
                  '  NetBios call couldn't be found.                                     EXPORTED
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Function pbNetBios Alias "pbNetBios" _
                        (  pncb As NTWRKCNTRLBLCK, bReturn As Byte) Export As Long
                  
                     Dim iLoadedHere As Long
                     Dim ptrProc As Dword
                  
                  
                     If g_Lib = 0 Then
                        g_Lib = LoadLibrary("netapi32.dll")
                        iLoadedHere = %True
                     Else
                        iLoadedHere = %False
                     End If
                  
                     If g_Lib = 0 Then
                        Function = %False
                     Else
                        ptrProc = GetProcAddress(g_Lib, "Netbios")
                        If ptrProc = 0 Then
                           Function = %False
                        Else
                           Call Dword ptrProc Using callTemplate_NetBios(pncb) To bReturn
                           Function = %True
                        End If
                     End If
                  
                     If iLoadedHere Then
                        If g_Lib <> 0 Then
                           FreeLibrary g_Lib
                           g_Lib = 0
                        End If
                     End If
                  
                  End Function
                  
                  '
                  '  GetMacAdd
                  '  Returns %True and sets sReturn to the MAC address if the call was
                  '  made successfully.
                  '  Returns %False if the pbNetBios call couldn't be made (either the
                  '  netapi32.dll library couldn't be loaded or the NetBios call
                  '  couldn't be found.
                  '                                                                      EXPORTED
                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  Function GetMacAdd Alias "GetMacAdd" (sReturn As String) Export As Long
                  
                     Dim NCB(0) As NTWRKCNTRLBLCK
                     Dim Status(0) As NET_STATUS
                     Dim LanEnum(0) As LANA_ENUM
                     Dim bReturn As Byte
                     Dim sMacAddress As String
                     Dim iCount As Integer
                     Dim sHex As String
                  
                     '- Make sure we start with an empty
                     '  string.
                     '
                     sReturn = ""
                  
                     '- Enumerate Lana Numbers
                     NCB(0).ncb_command = %NCBENUM
                     NCB(0).ncb_buffer = VarPtr(LanEnum(0))
                     NCB(0).ncb_length = Len(LanEnum(0))
                     If pbNetBios(NCB(0), bReturn) = %False Then
                        Function = %False
                        Exit Function
                     End If
                  
                     '- Reset NCB
                     NCB(0).ncb_command = %NCBRESET
                     NCB(0).ncb_lana_num = LanEnum(0).lana(0)
                     If pbNetBios(NCB(0), bReturn) = %False Then
                        Function = %False
                        Exit Function
                     End If
                  
                     '- Get Adapter Status
                     NCB(0).ncb_command = %NCBASTAT
                     NCB(0).ncb_lana_num = LanEnum(0).lana(0)
                     NCB(0).ncb_callname(0) = 42
                     For iCount = 1 To 14
                        NCB(0).ncb_callname(iCount) = 32
                     Next iCount
                     NCB(0).ncb_callname(15) = 0
                     NCB(0).ncb_buffer = VarPtr(Status(0))
                     NCB(0).ncb_length = Len(Status(0))
                  
                     If pbNetBios(NCB(0), bReturn) = %False Then
                        Function = %False
                        Exit Function
                     End If
                  
                     '- Format MacAddress
                     sMacAddress = ""
                     sHex = Hex$(Status(0).Adapter.adapter_address(0))
                     If Len(sHex) = 1 Then
                        sHex = "0" & sHex
                     End If
                     sMacAddress = sHex & "-"
                     For iCount = 1 To 5
                        sHex = Hex$(Status(0).Adapter.adapter_address(iCount))
                        If Len(sHex) = 1 Then
                           sHex = "0" & sHex
                        End If
                        sMacAddress = sMacAddress & sHex & "-"
                     Next iCount
                  
                     sReturn = Left$(sMacAddress, Len(sMacAddress) - 1)
                     Function = %True
                  
                  End Function
                  #EndIf
                  Don Dickinson
                  www.greatwebdivide.com

                  Comment


                  • #10
                    Originally posted by John McWilliams View Post
                    What's nice is ... and serve to push someone else along to explore the API.
                    I found it nice, because I understood it ;-) ...
                    and could get my code working under Xbase++
                    Regards,
                    Hubert

                    ------------------------------------
                    http://familie-brandel.de/index_e.html

                    Comment


                    • #11
                      Get MAC from the WAN, not only from LAN

                      Hi,

                      In the same way, in a WAN network it's possible to pick up the MAC address with powerbasic, LAN i have no problem ... for WAN i dont found anything on the forum ....

                      With dos you can do a ping 10.10.10.1 (or hostname) from 10.1.1.1 (your LAN) and after nbtstat -A 10.10.10.1 (or hostname) you will see the mac address ....

                      My software working fine with lan ... but wan ... no way !

                      Someone have an idea ?????

                      Thank you for your help.

                      Comment


                      • #12
                        My question would be, why should it be easy to get the mac address from a wan location and why would it matter except in a special situation of gathering information. It would seem there would need to be software written for special purposes to disclose that information.
                        p purvis

                        Comment


                        • #13
                          Hi Paul,

                          That's why i'm asking the question :laugh:, we have to do a full and partial invetentory and we have more than 50 vlan and 8000 pc, manually it's a little bit hard

                          Some ideas ????

                          Thank you for help

                          Comment


                          • #14
                            Originally posted by Guy Fournier View Post
                            Hi Paul,

                            That's why i'm asking the question :laugh:, we have to do a full and partial invetentory and we have more than 50 vlan and 8000 pc, manually it's a little bit hard

                            Some ideas ????

                            Thank you for help
                            AIDA32, if you can find it. I have a copy of it that I will email you if need be. Send me a PM if you can't find it with your e-mail. I can't recall the exact size but I seem to remember that's it has a pretty small footprint.

                            Comment


                            • #15
                              Hi John,

                              I found the software, thank you, good software, but how can i read from WAN?, the only bug i have with my software it's because can't passing the out the LAN ...

                              Thank you for the reply.

                              Comment


                              • #16
                                Originally posted by Guy Fournier View Post
                                Hi John,

                                I found the software, thank you, good software, but how can i read from WAN?, the only bug i have with my software it's because can't passing the out the LAN ...

                                Thank you for the reply.
                                If you can't get outside of your VLAN then the network guys have ports blocked at the switches/firewalls. If that's the case you're SOL. Someone will have to make a decision..... do you want to be able to remotely inventory or do you want things locked down. You've done everything you can do and it's up to them to open ports or do a seat-by-seat inventory. 8000 workstations huh, get comfortable shoes and an elevator key.

                                Comment


                                • #17
                                  Originally posted by Don Dickinson View Post
                                  below is the code i use to get mac (see comment notes for origin). haven't used it in a while, but it worked ok as i remember:
                                  Code:
                                  '
                                  '  pb_mac.inc
                                  '
                                  '  Functions to return the mac address of a network card.
                                  '  It calls dynamically (call dword) in case netapi32.dll isn't
                                  '  installed on the system.
                                  '
                                  '  VB code posted by Dave Navarro
                                  '  Original author unknown
                                  '  Translated from VB to PB by Don Dickinson
                                  '
                                  '  Functions exported:
                                  '     initNetBios
                                  '     freeNetBios
                                  '     pbNetBios      - note:  don't try to call this NetBios as that
                                  '                             call is already defined in win32api.inc.
                                  '                             our definition here is different.
                                  '     GetMacAdd
                                  '
                                  '  Dependencies
                                  '     This module depends on win32api.inc (not included here)
                                  '
                                  #If Not %Def(%PB_MAC_INC)
                                  %PB_MAC_INC = 1
                                  
                                  '
                                  '  Constants
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  %NCBENUM = &H37      'enumerate lana numbers
                                  %NCBRESET = &H32     'reset
                                  %NCBASTAT = &H33     'adapter status
                                  
                                  '
                                  '  Structures
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  '
                                  '- Network Control Block
                                  Type NTWRKCNTRLBLCK
                                     ncb_command As Byte              'command code
                                     ncb_retcode As Byte              'return code
                                     ncb_lsn As Byte                  'local session number
                                     ncb_num As Byte                  'number of our network name
                                     ncb_buffer As Long               'address of message buffer
                                     ncb_length As Integer            'size of message buffer
                                     ncb_callname(0 To 15) As Byte    'blank-padded name of remote
                                     ncb_name(0 To 15) As Byte        'our blank-padded netname
                                     ncb_rto As Byte                  'rcv timeout/retry count
                                     ncb_sto As Byte                  'send timeout/sys timeout
                                     lpFunc As Long                   'POST routine address
                                     ncb_lana_num As Byte             'lana (adapter) number
                                     ncb_cmd_cplt As Byte             '0xff => command pending
                                     ncb_reserve(0 To 9) As Byte      'reserved, used by BIOS
                                     ncb_event As Long                'HANDLE to Win32 event which will be set to the signalled
                                  End Type                            'state when an ASYNCH command completes
                                  Type LANA_ENUM
                                     length As Byte
                                     lana(0 To 256) As Byte
                                  End Type
                                  
                                  '- Structure returned to the NCB command NCBSTAT is ADAPTER_STATUS
                                  '  followed by an array of NAME_BUFFER structures => NET_STATUS
                                  '
                                  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
                                  
                                  Type NAME_BUFFER
                                     name_(0 To 15) As Byte
                                     name_num As Byte
                                     name_flags As Byte
                                  End Type
                                  
                                  Type NET_STATUS
                                     Adapter As ADAPTER_STATUS
                                     NameBuffer(30) As NAME_BUFFER
                                  End Type
                                  
                                  '
                                  '  Call DWORD template
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Declare Function callTemplate_NetBios(pncb As NTWRKCNTRLBLCK) As Byte
                                  
                                  
                                  '
                                  '  Global Variables
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Global g_Lib As Long
                                  
                                  '
                                  '  initNetBios
                                  '  This loads the netbios dll into memory. You don't have to call this function,
                                  '  but it may speed up your program if you have to make many netbios calls
                                  '  as it loads the dll into memory so it doesn't get loaded and unloaded
                                  '  on each call to NetBios.
                                  '
                                  '  IMPORTANT: If you Call this function, you must call freeNetBios before
                                  '             your program ends to unload the netapi32.dll library.
                                  '
                                  '  RETURNS: %True if successful, %False if not.                        EXPORTED
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Function initNetBios Alias "initNetBios" Export As Long
                                  
                                     If g_Lib = 0 Then
                                        g_Lib = LoadLibrary("netapi32.dll")
                                        If g_Lib Then
                                           Function = %True
                                        Else
                                           Function = %False
                                        End If
                                     Else
                                  
                                        '- It's already initialized
                                        Function = %True
                                     End If
                                  
                                  End Function
                                  
                                  '
                                  '  freeNetBios
                                  '  This function unloads the netbios dll. If you call initNetBios, you
                                  '  must call this before your program ends.                            EXPORTED
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Sub freeNetBios Alias "freeNetBios" Export
                                     If g_Lib Then
                                        FreeLibrary g_Lib
                                        g_Lib = 0
                                     End If
                                  End Sub
                                  
                                  '
                                  '  pbNetBios
                                  '  Dynamically load the netapi32.dll and CALL DWORD wrapper for the
                                  '  NetBios command exported from it.
                                  '  Returns %True if the library could be loaded. The returned byte from
                                  '  the NetBios api call is returned in the bReturn parameter.
                                  '  Returns %False if netapi32.dll couldn't be loaded or the
                                  '  NetBios call couldn't be found.                                     EXPORTED
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Function pbNetBios Alias "pbNetBios" _
                                        (  pncb As NTWRKCNTRLBLCK, bReturn As Byte) Export As Long
                                  
                                     Dim iLoadedHere As Long
                                     Dim ptrProc As Dword
                                  
                                  
                                     If g_Lib = 0 Then
                                        g_Lib = LoadLibrary("netapi32.dll")
                                        iLoadedHere = %True
                                     Else
                                        iLoadedHere = %False
                                     End If
                                  
                                     If g_Lib = 0 Then
                                        Function = %False
                                     Else
                                        ptrProc = GetProcAddress(g_Lib, "Netbios")
                                        If ptrProc = 0 Then
                                           Function = %False
                                        Else
                                           Call Dword ptrProc Using callTemplate_NetBios(pncb) To bReturn
                                           Function = %True
                                        End If
                                     End If
                                  
                                     If iLoadedHere Then
                                        If g_Lib <> 0 Then
                                           FreeLibrary g_Lib
                                           g_Lib = 0
                                        End If
                                     End If
                                  
                                  End Function
                                  
                                  '
                                  '  GetMacAdd
                                  '  Returns %True and sets sReturn to the MAC address if the call was
                                  '  made successfully.
                                  '  Returns %False if the pbNetBios call couldn't be made (either the
                                  '  netapi32.dll library couldn't be loaded or the NetBios call
                                  '  couldn't be found.
                                  '                                                                      EXPORTED
                                  '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                  Function GetMacAdd Alias "GetMacAdd" (sReturn As String) Export As Long
                                  
                                     Dim NCB(0) As NTWRKCNTRLBLCK
                                     Dim Status(0) As NET_STATUS
                                     Dim LanEnum(0) As LANA_ENUM
                                     Dim bReturn As Byte
                                     Dim sMacAddress As String
                                     Dim iCount As Integer
                                     Dim sHex As String
                                  
                                     '- Make sure we start with an empty
                                     '  string.
                                     '
                                     sReturn = ""
                                  
                                     '- Enumerate Lana Numbers
                                     NCB(0).ncb_command = %NCBENUM
                                     NCB(0).ncb_buffer = VarPtr(LanEnum(0))
                                     NCB(0).ncb_length = Len(LanEnum(0))
                                     If pbNetBios(NCB(0), bReturn) = %False Then
                                        Function = %False
                                        Exit Function
                                     End If
                                  
                                     '- Reset NCB
                                     NCB(0).ncb_command = %NCBRESET
                                     NCB(0).ncb_lana_num = LanEnum(0).lana(0)
                                     If pbNetBios(NCB(0), bReturn) = %False Then
                                        Function = %False
                                        Exit Function
                                     End If
                                  
                                     '- Get Adapter Status
                                     NCB(0).ncb_command = %NCBASTAT
                                     NCB(0).ncb_lana_num = LanEnum(0).lana(0)
                                     NCB(0).ncb_callname(0) = 42
                                     For iCount = 1 To 14
                                        NCB(0).ncb_callname(iCount) = 32
                                     Next iCount
                                     NCB(0).ncb_callname(15) = 0
                                     NCB(0).ncb_buffer = VarPtr(Status(0))
                                     NCB(0).ncb_length = Len(Status(0))
                                  
                                     If pbNetBios(NCB(0), bReturn) = %False Then
                                        Function = %False
                                        Exit Function
                                     End If
                                  
                                     '- Format MacAddress
                                     sMacAddress = ""
                                     sHex = Hex$(Status(0).Adapter.adapter_address(0))
                                     If Len(sHex) = 1 Then
                                        sHex = "0" & sHex
                                     End If
                                     sMacAddress = sHex & "-"
                                     For iCount = 1 To 5
                                        sHex = Hex$(Status(0).Adapter.adapter_address(iCount))
                                        If Len(sHex) = 1 Then
                                           sHex = "0" & sHex
                                        End If
                                        sMacAddress = sMacAddress & sHex & "-"
                                     Next iCount
                                  
                                     sReturn = Left$(sMacAddress, Len(sMacAddress) - 1)
                                     Function = %True
                                  
                                  End Function
                                  #EndIf
                                  You always show us all up with your elegant (not to mention reusable) code

                                  Comment


                                  • #18
                                    The code conflicts with the current win32api.inc. Therefore, I have made some changes to get it working with the new compilers:

                                    Code:
                                    '
                                    '  pb_mac.inc
                                    '
                                    '  Functions to return the mac address of a network card.
                                    '  It calls dynamically (call dword) in case netapi32.dll isn't
                                    '  installed on the system.
                                    '
                                    '  VB code posted by Dave Navarro
                                    '  Original author unknown
                                    '  Translated from VB to PB by Don Dickinson
                                    '
                                    '  Functions exported:
                                    '     initNetBios
                                    '     freeNetBios
                                    '     pbNetBios      - note:  don't try to call this NetBios as that
                                    '                             call is already defined in win32api.inc.
                                    '                             our definition here is different.
                                    '     GetMacAdd
                                    '
                                    '  Dependencies
                                    '     This module depends on win32api.inc (not included here)
                                    '
                                    #INCLUDE "C:\pbwin90\winapi\win32api.inc"
                                    
                                    #If Not %Def(%PB_MAC_INC)
                                    %PB_MAC_INC = 1
                                    
                                    '
                                    '  Constants
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    %NCBENUM = &H37      'enumerate lana numbers
                                    %NCBRESET = &H32     'reset
                                    %NCBASTAT = &H33     'adapter status
                                    
                                    '
                                    '  Structures
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    '
                                    '- Network Control Block
                                    'Type NTWRKCNTRLBLCK
                                    '   ncb_command As Byte              'command code
                                    '   ncb_retcode As Byte              'return code
                                    '   ncb_lsn As Byte                  'local session number
                                    '   ncb_num As Byte                  'number of our network name
                                    '   ncb_buffer As Long               'address of message buffer
                                    '   ncb_length As Integer            'size of message buffer
                                    '   ncb_callname(0 To 15) As Byte    'blank-padded name of remote
                                    '   ncb_name(0 To 15) As Byte        'our blank-padded netname
                                    '   ncb_rto As Byte                  'rcv timeout/retry count
                                    '   ncb_sto As Byte                  'send timeout/sys timeout
                                    '   lpFunc As Long                   'POST routine address
                                    '   ncb_lana_num As Byte             'lana (adapter) number
                                    '   ncb_cmd_cplt As Byte             '0xff => command pending
                                    '   ncb_reserve(0 To 9) As Byte      'reserved, used by BIOS
                                    '   ncb_event As Long                'HANDLE to Win32 event which will be set to the signalled
                                    'End Type                            'state when an ASYNCH command completes
                                    'Type LANA_ENUM
                                    '   length As Byte
                                    '   lana(0 To 256) As Byte
                                    'End Type
                                    
                                    '- Structure returned to the NCB command NCBSTAT is ADAPTER_STATUS
                                    '  followed by an array of NAME_BUFFER structures => NET_STATUS
                                    '
                                    '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
                                    
                                    'Type NAME_BUFFER
                                    '   name_(0 To 15) As Byte
                                    '   name_num As Byte
                                    '   name_flags As Byte
                                    'End Type
                                    
                                    Type NET_STATUS
                                       Adapter As ADAPTER_STATUS
                                    '   NameBuffer(30) As NAME_BUFFER
                                       NameBuffer(0 To 29) As NAME_BUFFER
                                    End Type
                                    
                                    '
                                    '  Call DWORD template
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Declare Function callTemplate_NetBios(pncb As NCB) As Byte
                                    
                                    
                                    '
                                    '  Global Variables
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Global g_Lib As Long
                                    
                                    '
                                    '  initNetBios
                                    '  This loads the netbios dll into memory. You don't have to call this function,
                                    '  but it may speed up your program if you have to make many netbios calls
                                    '  as it loads the dll into memory so it doesn't get loaded and unloaded
                                    '  on each call to NetBios.
                                    '
                                    '  IMPORTANT: If you Call this function, you must call freeNetBios before
                                    '             your program ends to unload the netapi32.dll library.
                                    '
                                    '  RETURNS: %True if successful, %False if not.                        EXPORTED
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Function initNetBios Alias "initNetBios" Export As Long
                                    
                                       If g_Lib = 0 Then
                                          g_Lib = LoadLibrary("netapi32.dll")
                                          If g_Lib Then
                                             Function = %True
                                          Else
                                             Function = %False
                                          End If
                                       Else
                                    
                                          '- It's already initialized
                                          Function = %True
                                       End If
                                    
                                    End Function
                                    
                                    '
                                    '  freeNetBios
                                    '  This function unloads the netbios dll. If you call initNetBios, you
                                    '  must call this before your program ends.                            EXPORTED
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Sub freeNetBios Alias "freeNetBios" Export
                                       If g_Lib Then
                                          FreeLibrary g_Lib
                                          g_Lib = 0
                                       End If
                                    End Sub
                                    
                                    '
                                    '  pbNetBios
                                    '  Dynamically load the netapi32.dll and CALL DWORD wrapper for the
                                    '  NetBios command exported from it.
                                    '  Returns %True if the library could be loaded. The returned byte from
                                    '  the NetBios api call is returned in the bReturn parameter.
                                    '  Returns %False if netapi32.dll couldn't be loaded or the
                                    '  NetBios call couldn't be found.                                     EXPORTED
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Function pbNetBios Alias "pbNetBios" _
                                          (  pncb As NCB, bReturn As Byte) Export As Long
                                    
                                       Dim iLoadedHere As Long
                                       Dim ptrProc As Dword
                                    
                                    
                                       If g_Lib = 0 Then
                                          g_Lib = LoadLibrary("netapi32.dll")
                                          iLoadedHere = %True
                                       Else
                                          iLoadedHere = %False
                                       End If
                                    
                                       If g_Lib = 0 Then
                                          Function = %False
                                       Else
                                          ptrProc = GetProcAddress(g_Lib, "Netbios")
                                          If ptrProc = 0 Then
                                             Function = %False
                                          Else
                                             Call Dword ptrProc Using callTemplate_NetBios(pncb) To bReturn
                                             Function = %True
                                          End If
                                       End If
                                    
                                       If iLoadedHere Then
                                          If g_Lib <> 0 Then
                                             FreeLibrary g_Lib
                                             g_Lib = 0
                                          End If
                                       End If
                                    
                                    End Function
                                    
                                    '
                                    '  GetMacAdd
                                    '  Returns %True and sets sReturn to the MAC address if the call was
                                    '  made successfully.
                                    '  Returns %False if the pbNetBios call couldn't be made (either the
                                    '  netapi32.dll library couldn't be loaded or the NetBios call
                                    '  couldn't be found.
                                    '                                                                      EXPORTED
                                    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                    Function GetMacAdd Alias "GetMacAdd" (sReturn As String) Export As Long
                                    
                                       Dim tNCB(0) As NCB
                                       Dim Status(0) As NET_STATUS
                                       Dim LanEnum(0) As LANA_ENUM
                                       Dim bReturn As Byte
                                       Dim sMacAddress As String
                                       Dim iCount As Integer
                                       Dim sHex As String
                                    
                                       '- Make sure we start with an empty
                                       '  string.
                                       '
                                       sReturn = ""
                                    
                                       '- Enumerate Lana Numbers
                                       tNCB(0).ncb_command = %NCBENUM
                                       tNCB(0).ncb_buffer = VarPtr(LanEnum(0))
                                       tNCB(0).ncb_length = Len(LanEnum(0))
                                       If pbNetBios(tNCB(0), bReturn) = %False Then
                                          Function = %False
                                          Exit Function
                                       End If
                                    
                                       '- Reset NCB
                                       tNCB(0).ncb_command = %NCBRESET
                                       tNCB(0).ncb_lana_num = LanEnum(0).lana(0)
                                       If pbNetBios(tNCB(0), bReturn) = %False Then
                                          Function = %False
                                          Exit Function
                                       End If
                                    
                                       '- Get Adapter Status
                                       tNCB(0).ncb_command = %NCBASTAT
                                       tNCB(0).ncb_lana_num = LanEnum(0).lana(0)
                                    '   tNCB(0).ncb_callname(0) = 42
                                    '   For iCount = 1 To 14
                                    '      tNCB(0).ncb_callname(iCount) = 32
                                    '   Next iCount
                                    '   tNCB(0).ncb_callname(15) = 0
                                       tNCB(0).ncb_callname = "*" & SPACE$(14) & $NUL
                                       tNCB(0).ncb_buffer = VarPtr(Status(0))
                                       tNCB(0).ncb_length = Len(Status(0))
                                    
                                       If pbNetBios(tNCB(0), bReturn) = %False Then
                                          Function = %False
                                          Exit Function
                                       End If
                                    
                                       '- Format MacAddress
                                       sMacAddress = ""
                                       sHex = Hex$(Status(0).Adapter.adapter_address(0))
                                       If Len(sHex) = 1 Then
                                          sHex = "0" & sHex
                                       End If
                                       sMacAddress = sHex & "-"
                                       For iCount = 1 To 5
                                          sHex = Hex$(Status(0).Adapter.adapter_address(iCount))
                                          If Len(sHex) = 1 Then
                                             sHex = "0" & sHex
                                          End If
                                          sMacAddress = sMacAddress & sHex & "-"
                                       Next iCount
                                    
                                       sReturn = Left$(sMacAddress, Len(sMacAddress) - 1)
                                       Function = %True
                                    
                                    End Function
                                    #EndIf
                                    
                                    
                                    FUNCTION PBMAIN
                                    
                                       LOCAL s AS STRING
                                       GetMacAdd(s)
                                       MSGBOX s
                                       
                                    END FUNCTION
                                    Here is a link to a Microsoft article about the subject:
                                    http://support.microsoft.com/kb/118623

                                    Note The Netbios function is not available for use on Windows Vista, Windows Server 2008, and subsequent versions of the operating system.
                                    Last edited by José Roca; 13 Mar 2009, 11:48 PM.
                                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                                    Comment


                                    • #19
                                      Hi John and José,

                                      For John:

                                      My big question is, if i can do this way (by dos and i have a good info):

                                      "With dos you can do a ping 10.10.10.1 (or hostname) from 10.1.1.1 (your LAN) and after nbtstat -A 10.10.10.1 (or hostname) you will see the mac address ...."

                                      What powerbasic can do for me? i'm not a geek with powerbaisc, but i can do that with my software on LAN and dos,

                                      I can't confirm if the cisco switch are full lock, i do a big job last summer with 1600 flat screens, factory bug, replacing thoses screens it's a lot of problems in my office

                                      Some areas we have to do manually the job, no access to this vlan, i have good shoes :laugh:

                                      For José:

                                      Tomorrow, i will check your way to get the mac, very good code ...

                                      Thank you for your help !

                                      Guy

                                      Comment


                                      • #20
                                        One quick way to find out your MAC address is to call the UuidCreateSequential function (needs Windows 2000 Professional or higher), that creates GUIDs using the MAC address of a machine's Ethernet card.

                                        Code:
                                        ' SED_PBWIN
                                        #COMPILE EXE
                                        #DIM ALL
                                        
                                        DECLARE FUNCTION UuidCreateSequential LIB "RPCRT4.DLL" ALIAS "UuidCreateSequential" ( _
                                           BYREF GUID _                        ' __out UUID * Uuid
                                         ) AS DWORD                            ' RPC_STATUS
                                        
                                        FUNCTION PBMAIN () AS LONG
                                        
                                           LOCAL g AS GUID
                                           LOCAL sMAC AS STRING
                                           UuidCreateSequential(g)
                                           sMAC = MID$(GUIDTXT$(g), 26, 12)
                                           MSGBOX sMAC
                                        
                                        END FUNCTION
                                        Forum: http://www.jose.it-berater.org/smfforum/index.php

                                        Comment

                                        Working...
                                        X