Network adapter question: how to put ip address and MAC addresses together 
Author Message
 Network adapter question: how to put ip address and MAC addresses together

I am able to get the ip addresses for each of the active network adapters by
using code based on the dll declared:
    Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal
Hostname$) As Long

I am also able to get the MAC addresses for each of the adapters by using
code based on the dll declared:
    Public Declare Function Netbios Lib "netapi32.dll" (pncb As
NET_CONTROL_BLOCK) As Byte

I am not able to put the two together.  That is, I would like to know which
MAC address goes with which ip address.

Anyone know how to do this?

Thanks.



Sat, 17 Apr 2004 07:32:43 GMT  
 Network adapter question: how to put ip address and MAC addresses together
Post the code here that you are using.

        -Andy (CEO, Dynamic Bits)
         http://dynamicbits.com


Quote:
> I am able to get the ip addresses for each of the active network adapters
by
> using code based on the dll declared:

[snip]


Sun, 18 Apr 2004 15:40:01 GMT  
 Network adapter question: how to put ip address and MAC addresses together
Thanks for your interest.  Thanks for any help you could provide.

For now I have the code in 2 different modules.
I am not able to correlate the adapter  MAC address with the IP address.

'======================================================
'Module below is used to return a collection of Adapter MAC Addresses:
'=======================================================
Option Explicit

Private Const NCBASTAT As Long = &H33
Private Const NCBNAMSZ As Long = 16
Private Const HEAP_ZERO_MEMORY As Long = &H8
Private Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
Private Const NCBRESET As Long = &H32
Private Const NCBENUM = &H37
Private Const MAX_LANA = 254

Private Type NET_CONTROL_BLOCK  'NCB
   ncb_command    As Byte
   ncb_retcode    As Byte
   ncb_lsn        As Byte
   ncb_num        As Byte
   ncb_buffer     As Long
   ncb_length     As Integer
   ncb_callname   As String * NCBNAMSZ
   ncb_name       As String * NCBNAMSZ
   ncb_rto        As Byte
   ncb_sto        As Byte
   ncb_post       As Long
   ncb_lana_num   As Byte
   ncb_cmd_cplt   As Byte
   ncb_reserve(9) As Byte ' Reserved, must be 0
   ncb_event      As Long
End Type

Private 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
   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        As String * NCBNAMSZ
   name_num    As Integer
   name_flags  As Integer
End Type

Private Type ASTAT
   Adapt          As ADAPTER_STATUS
   NameBuff(30)   As NAME_BUFFER
End Type

Private Type LANA_ENUM
   Length As Integer
   lana(MAX_LANA) As Integer
End Type

Private Declare Function Netbios Lib "netapi32.dll" (pncb As
NET_CONTROL_BLOCK) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest
As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal
dwFlags As Long, lpMem As Any) As Long

Sub GetAdapterMacAddress(colAdapterMacAddress As Collection)
   Dim colPhysicalAddress As New Collection
   Dim PhysicalAddress$, tmp$
   Dim pAstat&
   Dim Ncb As NET_CONTROL_BLOCK
   Dim Ast As ASTAT
   Dim LEnum As LANA_ENUM
   Dim pLEnum As Long
   Dim i%, ByteNum%
   Dim InList As Boolean
   Dim NumDialups%

   NumDialups = 0

   Ncb.ncb_length = Len(LEnum)

   pLEnum = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
HEAP_ZERO_MEMORY, Ncb.ncb_length)
   If pLEnum = 0 Then
      MsgBox "memory allocation failed!", vbCritical
      Exit Sub
   End If

   Ncb.ncb_buffer = pLEnum
   Ncb.ncb_command = NCBENUM
   Call Netbios(Ncb)
   CopyMemory LEnum, Ncb.ncb_buffer, Len(LEnum)
   HeapFree GetProcessHeap(), 0, pLEnum

   For i = 0 To LEnum.Length - 1
      If LEnum.lana(i) < 256 Then

         Ncb.ncb_lana_num = LEnum.lana(i)
         Ncb.ncb_command = NCBRESET
         Call Netbios(Ncb)
         HeapFree GetProcessHeap(), 0, pAstat 'bsm

         Ncb.ncb_callname = "*               "
         Ncb.ncb_command = NCBASTAT

         Ncb.ncb_lana_num = LEnum.lana(i)
         Ncb.ncb_length = Len(Ast)

         pAstat = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
HEAP_ZERO_MEMORY, Ncb.ncb_length)

         If pAstat = 0 Then
            MsgBox "memory allocation failed!", vbCritical
            Exit Sub
         End If

         Ncb.ncb_buffer = pAstat
         Call Netbios(Ncb)

         CopyMemory Ast, Ncb.ncb_buffer, Len(Ast)

         PhysicalAddress$ = ""
         For ByteNum = 0 To 5
            If Len(PhysicalAddress$) > 0 Then PhysicalAddress$ =
PhysicalAddress$ & ":"
            PhysicalAddress$ = PhysicalAddress$ & Right$("00" &
Hex$(Ast.Adapt.adapter_address(ByteNum)), 2)
         Next

         If PhysicalAddress$ = "44:45:53:54:00:00" Then
            NumDialups = NumDialups + 1
            PhysicalAddress$ = "dialup" & Trim$(NumDialups)
         End If

         On Error GoTo AlreadyInList
         colPhysicalAddress.Add PhysicalAddress$, PhysicalAddress$
         On Error GoTo 0

         HeapFree GetProcessHeap(), 0, pAstat
      End If
   Next i
   Set colAdapterMacAddress = colPhysicalAddress
   Set colPhysicalAddress = Nothing
   Exit Sub

AlreadyInList:
   InList = True
   Resume Next
End Sub

'=====================================================
'Module below is used to return a collection of Active Adapter Ip Addresses
'=====================================================
Option Explicit

Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128

Private Type hostent
   hName As Long
   hAliases As Long
   hAddrType As Integer
   hLength As Integer
   haddrlist As Long
End Type

Private Type WSADATA
   wVersion As Integer
   wHighVersion As Integer
   szDescription(0 To WSADescription_Len) As Byte
   szSystemStatus(0 To WSASYS_Status_Len) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpszVendorInfo As Long
End Type

Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal
wVersionRequired As Integer, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal HostName$,
ByVal HostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal HostName$)
As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal
hpvSource&, ByVal cbCopy&)

Function hibyte(ByVal wParam As Integer)
   hibyte = wParam \ &H100 And &HFF&
End Function

Function lobyte(ByVal wParam As Integer)
   lobyte = wParam And &HFF&
End Function

Sub SocketsInitialize()
   Dim WSAD As WSADATA
   Dim iReturn As Integer
   Dim sLowByte As String, sHighByte As String, sMsg As String

   iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

   If iReturn <> 0 Then
      MsgBox "Winsock.dll is not responding."
      End
   End If

   If lobyte(WSAD.wVersion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wVersion) =
WS_VERSION_MAJOR And hibyte(WSAD.wVersion) < WS_VERSION_MINOR) Then
      sHighByte = Trim$(Str$(hibyte(WSAD.wVersion)))
      sLowByte = Trim$(Str$(lobyte(WSAD.wVersion)))
      sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
      sMsg = sMsg & " is not supported by winsock.dll "
      MsgBox sMsg
      End
   End If

      'iMaxSockets is not used in winsock 2. So the following check is only
      'necessary for winsock 1. If winsock 2 is requested,
      'the following check can be skipped.

   If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
      sMsg = "This application requires a minimum of "
      sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
      MsgBox sMsg
      End
   End If

   End Sub

Sub SocketsCleanup()
   Dim lReturn As Long

   lReturn = WSACleanup()

   If lReturn <> 0 Then
      MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup
"
      End
   End If

End Sub

Sub GetIpAdresses(ReturnHostname$, colHostIpAddresses As Collection)
   Dim HostName As String * 256
   Dim hostent_addr As Long
   Dim host As hostent
   Dim hostip_addr As Long
   Dim temp_ip_address() As Byte
   Dim i As Integer, NullPos As Integer
   Dim ip_address As String

   ReturnHostname$ = ""
   Set colHostIpAddresses = Nothing
   Set colHostIpAddresses = New Collection

   SocketsInitialize

   If gethostname(HostName, 256) = SOCKET_ERROR Then
      MsgBox "Windows Sockets error " & Str(WSAGetLastError())
      Exit Sub
   Else
      HostName$ = Trim$(HostName)
   End If

   hostent_addr = gethostbyname(HostName$)

   If hostent_addr = 0 Then
       MsgBox "Winsock.dll is not responding."
       Exit Sub
   End If

   RtlMoveMemory host, hostent_addr, LenB(host)
   RtlMoveMemory hostip_addr, host.haddrlist, 4

      'get all active IP addresses
   Do
      ReDim temp_ip_address(1 To host.hLength)
      RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

      For i = 1 To host.hLength
         ip_address = ip_address & temp_ip_address(i) & "."
      Next
      ip_address = Trim$(Mid$(ip_address, 1, Len(ip_address) - 1))

      colHostIpAddresses.Add ip_address, ip_address

      ip_address = ""
      host.haddrlist = host.haddrlist + LenB(host.haddrlist)
      RtlMoveMemory hostip_addr, host.haddrlist, 4
   Loop While (hostip_addr <> 0)

   SocketsCleanup

      'remove nulls from hostname
   NullPos = InStr(HostName$, Chr$(0))
   If NullPos > 1 Then HostName$ = Left$(HostName$, NullPos - 1)
   ReturnHostname$ = Trim$(HostName$)
End Sub

===============================================
An alternative way of getting the MAC addresses involves the declaration:
Private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As
Any, pOutBufLen As Long) As Long

But this does not help me correlate the MAC and IP addresses.

...

read more »



Mon, 19 Apr 2004 04:34:52 GMT  
 Network adapter question: how to put ip address and MAC addresses together
I don't know if this will help you (or work on all platforms), but you
can take a look at the following Registry Key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage

The "Bind" value (on my machine) contains the two adapters listed when
I type IPCONFIG.

then check the Values listed under this key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[ADAPTER_FROM_PREVIOUS_LIST_HERE]\Parameters\Tcpip

You can find the IP Addresses there.

Hope this helps.  PS:  Let me know if this works.

Good Luck,

Mike

Quote:

> Thanks for your interest.  Thanks for any help you could provide.

> For now I have the code in 2 different modules.
> I am not able to correlate the adapter  MAC address with the IP address.

> '======================================================
> 'Module below is used to return a collection of Adapter MAC Addresses:
> '=======================================================
> Option Explicit

> Private Const NCBASTAT As Long = &H33
> Private Const NCBNAMSZ As Long = 16
> Private Const HEAP_ZERO_MEMORY As Long = &H8
> Private Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
> Private Const NCBRESET As Long = &H32
> Private Const NCBENUM = &H37
> Private Const MAX_LANA = 254

> Private Type NET_CONTROL_BLOCK  'NCB
>    ncb_command    As Byte
>    ncb_retcode    As Byte
>    ncb_lsn        As Byte
>    ncb_num        As Byte
>    ncb_buffer     As Long
>    ncb_length     As Integer
>    ncb_callname   As String * NCBNAMSZ
>    ncb_name       As String * NCBNAMSZ
>    ncb_rto        As Byte
>    ncb_sto        As Byte
>    ncb_post       As Long
>    ncb_lana_num   As Byte
>    ncb_cmd_cplt   As Byte
>    ncb_reserve(9) As Byte ' Reserved, must be 0
>    ncb_event      As Long
> End Type

> Private 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
>    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        As String * NCBNAMSZ
>    name_num    As Integer
>    name_flags  As Integer
> End Type

> Private Type ASTAT
>    Adapt          As ADAPTER_STATUS
>    NameBuff(30)   As NAME_BUFFER
> End Type

> Private Type LANA_ENUM
>    Length As Integer
>    lana(MAX_LANA) As Integer
> End Type

> Private Declare Function Netbios Lib "netapi32.dll" (pncb As
> NET_CONTROL_BLOCK) As Byte
> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest
> As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
> Private Declare Function GetProcessHeap Lib "kernel32" () As Long
> Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
> ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
> Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal
> dwFlags As Long, lpMem As Any) As Long

> Sub GetAdapterMacAddress(colAdapterMacAddress As Collection)
>    Dim colPhysicalAddress As New Collection
>    Dim PhysicalAddress$, tmp$
>    Dim pAstat&
>    Dim Ncb As NET_CONTROL_BLOCK
>    Dim Ast As ASTAT
>    Dim LEnum As LANA_ENUM
>    Dim pLEnum As Long
>    Dim i%, ByteNum%
>    Dim InList As Boolean
>    Dim NumDialups%

>    NumDialups = 0

>    Ncb.ncb_length = Len(LEnum)

>    pLEnum = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> HEAP_ZERO_MEMORY, Ncb.ncb_length)
>    If pLEnum = 0 Then
>       MsgBox "memory allocation failed!", vbCritical
>       Exit Sub
>    End If

>    Ncb.ncb_buffer = pLEnum
>    Ncb.ncb_command = NCBENUM
>    Call Netbios(Ncb)
>    CopyMemory LEnum, Ncb.ncb_buffer, Len(LEnum)
>    HeapFree GetProcessHeap(), 0, pLEnum

>    For i = 0 To LEnum.Length - 1
>       If LEnum.lana(i) < 256 Then

>          Ncb.ncb_lana_num = LEnum.lana(i)
>          Ncb.ncb_command = NCBRESET
>          Call Netbios(Ncb)
>          HeapFree GetProcessHeap(), 0, pAstat 'bsm

>          Ncb.ncb_callname = "*               "
>          Ncb.ncb_command = NCBASTAT

>          Ncb.ncb_lana_num = LEnum.lana(i)
>          Ncb.ncb_length = Len(Ast)

>          pAstat = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> HEAP_ZERO_MEMORY, Ncb.ncb_length)

>          If pAstat = 0 Then
>             MsgBox "memory allocation failed!", vbCritical
>             Exit Sub
>          End If

>          Ncb.ncb_buffer = pAstat
>          Call Netbios(Ncb)

>          CopyMemory Ast, Ncb.ncb_buffer, Len(Ast)

>          PhysicalAddress$ = ""
>          For ByteNum = 0 To 5
>             If Len(PhysicalAddress$) > 0 Then PhysicalAddress$ =
> PhysicalAddress$ & ":"
>             PhysicalAddress$ = PhysicalAddress$ & Right$("00" &
> Hex$(Ast.Adapt.adapter_address(ByteNum)), 2)
>          Next

>          If PhysicalAddress$ = "44:45:53:54:00:00" Then
>             NumDialups = NumDialups + 1
>             PhysicalAddress$ = "dialup" & Trim$(NumDialups)
>          End If

>          On Error GoTo AlreadyInList
>          colPhysicalAddress.Add PhysicalAddress$, PhysicalAddress$
>          On Error GoTo 0

>          HeapFree GetProcessHeap(), 0, pAstat
>       End If
>    Next i
>    Set colAdapterMacAddress = colPhysicalAddress
>    Set colPhysicalAddress = Nothing
>    Exit Sub

> AlreadyInList:
>    InList = True
>    Resume Next
> End Sub

> '=====================================================
> 'Module below is used to return a collection of Active Adapter Ip Addresses
> '=====================================================
> Option Explicit

> Private Const WS_VERSION_REQD = &H101
> Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
> Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
> Private Const MIN_SOCKETS_REQD = 1
> Private Const SOCKET_ERROR = -1
> Private Const WSADescription_Len = 256
> Private Const WSASYS_Status_Len = 128

> Private Type hostent
>    hName As Long
>    hAliases As Long
>    hAddrType As Integer
>    hLength As Integer
>    haddrlist As Long
> End Type

> Private Type WSADATA
>    wVersion As Integer
>    wHighVersion As Integer
>    szDescription(0 To WSADescription_Len) As Byte
>    szSystemStatus(0 To WSASYS_Status_Len) As Byte
>    iMaxSockets As Integer
>    iMaxUdpDg As Integer
>    lpszVendorInfo As Long
> End Type

> Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
> Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal
> wVersionRequired As Integer, lpWSADATA As WSADATA) As Long
> Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

> Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal HostName$,
> ByVal HostLen As Long) As Long
> Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal HostName$)
> As Long
> Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal
> hpvSource&, ByVal cbCopy&)

> Function hibyte(ByVal wParam As Integer)
>    hibyte = wParam \ &H100 And &HFF&
> End Function

> Function lobyte(ByVal wParam As Integer)
>    lobyte = wParam And &HFF&
> End Function

> Sub SocketsInitialize()
>    Dim WSAD As WSADATA
>    Dim iReturn As Integer
>    Dim sLowByte As String, sHighByte As String, sMsg As String

>    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

>    If iReturn <> 0 Then
>       MsgBox "Winsock.dll is not responding."
>       End
>    End If

>    If lobyte(WSAD.wVersion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wVersion) =
> WS_VERSION_MAJOR And hibyte(WSAD.wVersion) < WS_VERSION_MINOR) Then
>       sHighByte = Trim$(Str$(hibyte(WSAD.wVersion)))
>       sLowByte = Trim$(Str$(lobyte(WSAD.wVersion)))
>       sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
>       sMsg = sMsg & " is not supported by winsock.dll "
>       MsgBox sMsg
>       End
>    End If

>       'iMaxSockets is not used in winsock 2. So the following check is only
>       'necessary for winsock 1. If winsock 2 is requested,
>       'the following check can be skipped.

>    If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
>       sMsg = "This application requires a minimum of "
>       sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
>       MsgBox sMsg
>       End
>    End If

>    End Sub

> Sub SocketsCleanup()
>    Dim lReturn As Long

>    lReturn = WSACleanup()

>    If lReturn <> 0 Then
>       MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup
> "
>       End
>    End If

> End Sub

> Sub GetIpAdresses(ReturnHostname$, colHostIpAddresses As Collection)
>    Dim HostName As String * 256
>    Dim hostent_addr As Long
>    Dim host As hostent
>    Dim hostip_addr As Long
>    Dim temp_ip_address() As Byte
>    Dim i As Integer, NullPos As Integer
>    Dim ip_address As String

>    ReturnHostname$ = ""
>    Set colHostIpAddresses = Nothing
>    Set colHostIpAddresses = New Collection

>    SocketsInitialize

>    If gethostname(HostName, 256) = SOCKET_ERROR Then
>       MsgBox "Windows Sockets error " & Str(WSAGetLastError())
>       Exit Sub
>    Else
>       HostName$ = Trim$(HostName)
>    End If

>    hostent_addr = gethostbyname(HostName$)

>    If hostent_addr = 0 Then
>        MsgBox "Winsock.dll is not responding."
>        Exit Sub

...

read more »



Tue, 20 Apr 2004 00:31:48 GMT  
 Network adapter question: how to put ip address and MAC addresses together
Thanks for the info.  These keys are not on my Windows Me machine.
 I have been searching for an api solution.  Someone recommened the SendArp
function in the iphlpapi dll.  I have not been able to get this to work on
my machine.

Thanks again.
--------------


I don't know if this will help you (or work on all platforms), but you
can take a look at the following Registry Key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage

The "Bind" value (on my machine) contains the two adapters listed when
I type IPCONFIG.

then check the Values listed under this key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[ADAPTER_FROM_PREVIOUS_
LIST_HERE]\Parameters\Tcpip

You can find the IP Addresses there.

Hope this helps.  PS:  Let me know if this works.

Good Luck,

Mike


Quote:
> Thanks for your interest.  Thanks for any help you could provide.

> For now I have the code in 2 different modules.
> I am not able to correlate the adapter  MAC address with the IP address.

> '======================================================
> 'Module below is used to return a collection of Adapter MAC Addresses:
> '=======================================================
> Option Explicit

> Private Const NCBASTAT As Long = &H33
> Private Const NCBNAMSZ As Long = 16
> Private Const HEAP_ZERO_MEMORY As Long = &H8
> Private Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
> Private Const NCBRESET As Long = &H32
> Private Const NCBENUM = &H37
> Private Const MAX_LANA = 254

> Private Type NET_CONTROL_BLOCK  'NCB
>    ncb_command    As Byte
>    ncb_retcode    As Byte
>    ncb_lsn        As Byte
>    ncb_num        As Byte
>    ncb_buffer     As Long
>    ncb_length     As Integer
>    ncb_callname   As String * NCBNAMSZ
>    ncb_name       As String * NCBNAMSZ
>    ncb_rto        As Byte
>    ncb_sto        As Byte
>    ncb_post       As Long
>    ncb_lana_num   As Byte
>    ncb_cmd_cplt   As Byte
>    ncb_reserve(9) As Byte ' Reserved, must be 0
>    ncb_event      As Long
> End Type

> Private 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
>    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        As String * NCBNAMSZ
>    name_num    As Integer
>    name_flags  As Integer
> End Type

> Private Type ASTAT
>    Adapt          As ADAPTER_STATUS
>    NameBuff(30)   As NAME_BUFFER
> End Type

> Private Type LANA_ENUM
>    Length As Integer
>    lana(MAX_LANA) As Integer
> End Type

> Private Declare Function Netbios Lib "netapi32.dll" (pncb As
> NET_CONTROL_BLOCK) As Byte
> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(hpvDest
> As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
> Private Declare Function GetProcessHeap Lib "kernel32" () As Long
> Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
> ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
> Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long,
ByVal
> dwFlags As Long, lpMem As Any) As Long

> Sub GetAdapterMacAddress(colAdapterMacAddress As Collection)
>    Dim colPhysicalAddress As New Collection
>    Dim PhysicalAddress$, tmp$
>    Dim pAstat&
>    Dim Ncb As NET_CONTROL_BLOCK
>    Dim Ast As ASTAT
>    Dim LEnum As LANA_ENUM
>    Dim pLEnum As Long
>    Dim i%, ByteNum%
>    Dim InList As Boolean
>    Dim NumDialups%

>    NumDialups = 0

>    Ncb.ncb_length = Len(LEnum)

>    pLEnum = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> HEAP_ZERO_MEMORY, Ncb.ncb_length)
>    If pLEnum = 0 Then
>       MsgBox "memory allocation failed!", vbCritical
>       Exit Sub
>    End If

>    Ncb.ncb_buffer = pLEnum
>    Ncb.ncb_command = NCBENUM
>    Call Netbios(Ncb)
>    CopyMemory LEnum, Ncb.ncb_buffer, Len(LEnum)
>    HeapFree GetProcessHeap(), 0, pLEnum

>    For i = 0 To LEnum.Length - 1
>       If LEnum.lana(i) < 256 Then

>          Ncb.ncb_lana_num = LEnum.lana(i)
>          Ncb.ncb_command = NCBRESET
>          Call Netbios(Ncb)
>          HeapFree GetProcessHeap(), 0, pAstat 'bsm

>          Ncb.ncb_callname = "*               "
>          Ncb.ncb_command = NCBASTAT

>          Ncb.ncb_lana_num = LEnum.lana(i)
>          Ncb.ncb_length = Len(Ast)

>          pAstat = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> HEAP_ZERO_MEMORY, Ncb.ncb_length)

>          If pAstat = 0 Then
>             MsgBox "memory allocation failed!", vbCritical
>             Exit Sub
>          End If

>          Ncb.ncb_buffer = pAstat
>          Call Netbios(Ncb)

>          CopyMemory Ast, Ncb.ncb_buffer, Len(Ast)

>          PhysicalAddress$ = ""
>          For ByteNum = 0 To 5
>             If Len(PhysicalAddress$) > 0 Then PhysicalAddress$ =
> PhysicalAddress$ & ":"
>             PhysicalAddress$ = PhysicalAddress$ & Right$("00" &
> Hex$(Ast.Adapt.adapter_address(ByteNum)), 2)
>          Next

>          If PhysicalAddress$ = "44:45:53:54:00:00" Then
>             NumDialups = NumDialups + 1
>             PhysicalAddress$ = "dialup" & Trim$(NumDialups)
>          End If

>          On Error GoTo AlreadyInList
>          colPhysicalAddress.Add PhysicalAddress$, PhysicalAddress$
>          On Error GoTo 0

>          HeapFree GetProcessHeap(), 0, pAstat
>       End If
>    Next i
>    Set colAdapterMacAddress = colPhysicalAddress
>    Set colPhysicalAddress = Nothing
>    Exit Sub

> AlreadyInList:
>    InList = True
>    Resume Next
> End Sub

> '=====================================================
> 'Module below is used to return a collection of Active Adapter Ip
Addresses
> '=====================================================
> Option Explicit

> Private Const WS_VERSION_REQD = &H101
> Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
> Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
> Private Const MIN_SOCKETS_REQD = 1
> Private Const SOCKET_ERROR = -1
> Private Const WSADescription_Len = 256
> Private Const WSASYS_Status_Len = 128

> Private Type hostent
>    hName As Long
>    hAliases As Long
>    hAddrType As Integer
>    hLength As Integer
>    haddrlist As Long
> End Type

> Private Type WSADATA
>    wVersion As Integer
>    wHighVersion As Integer
>    szDescription(0 To WSADescription_Len) As Byte
>    szSystemStatus(0 To WSASYS_Status_Len) As Byte
>    iMaxSockets As Integer
>    iMaxUdpDg As Integer
>    lpszVendorInfo As Long
> End Type

> Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
> Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal
> wVersionRequired As Integer, lpWSADATA As WSADATA) As Long
> Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

> Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal HostName$,
> ByVal HostLen As Long) As Long
> Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal HostName$)
> As Long
> Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal
> hpvSource&, ByVal cbCopy&)

> Function hibyte(ByVal wParam As Integer)
>    hibyte = wParam \ &H100 And &HFF&
> End Function

> Function lobyte(ByVal wParam As Integer)
>    lobyte = wParam And &HFF&
> End Function

> Sub SocketsInitialize()
>    Dim WSAD As WSADATA
>    Dim iReturn As Integer
>    Dim sLowByte As String, sHighByte As String, sMsg As String

>    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

>    If iReturn <> 0 Then
>       MsgBox "Winsock.dll is not responding."
>       End
>    End If

>    If lobyte(WSAD.wVersion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wVersion) =
> WS_VERSION_MAJOR And hibyte(WSAD.wVersion) < WS_VERSION_MINOR) Then
>       sHighByte = Trim$(Str$(hibyte(WSAD.wVersion)))
>       sLowByte = Trim$(Str$(lobyte(WSAD.wVersion)))
>       sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
>       sMsg = sMsg & " is not supported by winsock.dll "
>       MsgBox sMsg
>       End
>    End If

>       'iMaxSockets is not used in winsock 2. So the following check is
only
>       'necessary for winsock 1. If winsock 2 is requested,
>       'the following check can be skipped.

>    If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
>       sMsg = "This application requires a minimum of "
>       sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
>       MsgBox sMsg
>       End
>    End If

>    End Sub

> Sub SocketsCleanup()
>    Dim lReturn As Long

>    lReturn = WSACleanup()

>    If lReturn <> 0 Then
>       MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in
Cleanup
> "
>       End
>    End If

> End Sub

> Sub GetIpAdresses(ReturnHostname$, colHostIpAddresses As Collection)
>    Dim HostName As String * 256
>    Dim hostent_addr As Long
>    Dim host As hostent
>    Dim hostip_addr As Long
>    Dim temp_ip_address() As Byte
>    Dim i As Integer, NullPos As Integer
>    Dim ip_address As String

>    ReturnHostname$ = ""
>    Set colHostIpAddresses = Nothing
>    Set colHostIpAddresses = New Collection

>    SocketsInitialize

>    If

...

read more »



Wed, 21 Apr 2004 01:34:37 GMT  
 Network adapter question: how to put ip address and MAC addresses together
DD,

To get local ip addresses for all installed adapter, checkout the vb
code at (http://www.mvps.org/vbnet/code/network/getadaptersinfo-localipaddress...).
This code is based on the iphlpapi.dll, which is not loaded on all 9X
PCs.

If you want to program for all 9X PCs, checkout the sample code at
(http://tangentsoft.net/wskfaq/articles/wscontrol.html). The sample is
based on Winsock 2 or higher being installed.

Also checkout Microsoft article #Q160215 "HOWTO: Obtain the Host IP
Address Using Windows Sockets".

If you want to use the registry to obtain Microsoft TCP info, the key
is
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP. And
DHCP info is located in registry key
HKEY_LOCAL_MACHINE\System\CurrentControl\Services\VxD\DHCP.

PuterHead

Quote:

> Thanks for the info.  These keys are not on my Windows Me machine.
>  I have been searching for an api solution.  Someone recommened the SendArp
> function in the iphlpapi dll.  I have not been able to get this to work on
> my machine.

> Thanks again.
> --------------



> I don't know if this will help you (or work on all platforms), but you
> can take a look at the following Registry Key:

> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage

> The "Bind" value (on my machine) contains the two adapters listed when
> I type IPCONFIG.

> then check the Values listed under this key:

> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[ADAPTER_FROM_PREVIOUS_
> LIST_HERE]\Parameters\Tcpip

> You can find the IP Addresses there.

> Hope this helps.  PS:  Let me know if this works.

> Good Luck,

> Mike



> > Thanks for your interest.  Thanks for any help you could provide.

> > For now I have the code in 2 different modules.
> > I am not able to correlate the adapter  MAC address with the IP address.

> > '======================================================
> > 'Module below is used to return a collection of Adapter MAC Addresses:
> > '=======================================================
> > Option Explicit

> > Private Const NCBASTAT As Long = &H33
> > Private Const NCBNAMSZ As Long = 16
> > Private Const HEAP_ZERO_MEMORY As Long = &H8
> > Private Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
> > Private Const NCBRESET As Long = &H32
> > Private Const NCBENUM = &H37
> > Private Const MAX_LANA = 254

> > Private Type NET_CONTROL_BLOCK  'NCB
> >    ncb_command    As Byte
> >    ncb_retcode    As Byte
> >    ncb_lsn        As Byte
> >    ncb_num        As Byte
> >    ncb_buffer     As Long
> >    ncb_length     As Integer
> >    ncb_callname   As String * NCBNAMSZ
> >    ncb_name       As String * NCBNAMSZ
> >    ncb_rto        As Byte
> >    ncb_sto        As Byte
> >    ncb_post       As Long
> >    ncb_lana_num   As Byte
> >    ncb_cmd_cplt   As Byte
> >    ncb_reserve(9) As Byte ' Reserved, must be 0
> >    ncb_event      As Long
> > End Type

> > Private 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
> >    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        As String * NCBNAMSZ
> >    name_num    As Integer
> >    name_flags  As Integer
> > End Type

> > Private Type ASTAT
> >    Adapt          As ADAPTER_STATUS
> >    NameBuff(30)   As NAME_BUFFER
> > End Type

> > Private Type LANA_ENUM
> >    Length As Integer
> >    lana(MAX_LANA) As Integer
> > End Type

> > Private Declare Function Netbios Lib "netapi32.dll" (pncb As
> > NET_CONTROL_BLOCK) As Byte
> > Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
>  (hpvDest
> > As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
> > Private Declare Function GetProcessHeap Lib "kernel32" () As Long
> > Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long,
> > ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
> > Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long,
>  ByVal
> > dwFlags As Long, lpMem As Any) As Long

> > Sub GetAdapterMacAddress(colAdapterMacAddress As Collection)
> >    Dim colPhysicalAddress As New Collection
> >    Dim PhysicalAddress$, tmp$
> >    Dim pAstat&
> >    Dim Ncb As NET_CONTROL_BLOCK
> >    Dim Ast As ASTAT
> >    Dim LEnum As LANA_ENUM
> >    Dim pLEnum As Long
> >    Dim i%, ByteNum%
> >    Dim InList As Boolean
> >    Dim NumDialups%

> >    NumDialups = 0

> >    Ncb.ncb_length = Len(LEnum)

> >    pLEnum = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> > HEAP_ZERO_MEMORY, Ncb.ncb_length)
> >    If pLEnum = 0 Then
> >       MsgBox "memory allocation failed!", vbCritical
> >       Exit Sub
> >    End If

> >    Ncb.ncb_buffer = pLEnum
> >    Ncb.ncb_command = NCBENUM
> >    Call Netbios(Ncb)
> >    CopyMemory LEnum, Ncb.ncb_buffer, Len(LEnum)
> >    HeapFree GetProcessHeap(), 0, pLEnum

> >    For i = 0 To LEnum.Length - 1
> >       If LEnum.lana(i) < 256 Then

> >          Ncb.ncb_lana_num = LEnum.lana(i)
> >          Ncb.ncb_command = NCBRESET
> >          Call Netbios(Ncb)
> >          HeapFree GetProcessHeap(), 0, pAstat 'bsm

> >          Ncb.ncb_callname = "*               "
> >          Ncb.ncb_command = NCBASTAT

> >          Ncb.ncb_lana_num = LEnum.lana(i)
> >          Ncb.ncb_length = Len(Ast)

> >          pAstat = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or
> > HEAP_ZERO_MEMORY, Ncb.ncb_length)

> >          If pAstat = 0 Then
> >             MsgBox "memory allocation failed!", vbCritical
> >             Exit Sub
> >          End If

> >          Ncb.ncb_buffer = pAstat
> >          Call Netbios(Ncb)

> >          CopyMemory Ast, Ncb.ncb_buffer, Len(Ast)

> >          PhysicalAddress$ = ""
> >          For ByteNum = 0 To 5
> >             If Len(PhysicalAddress$) > 0 Then PhysicalAddress$ =
> > PhysicalAddress$ & ":"
> >             PhysicalAddress$ = PhysicalAddress$ & Right$("00" &
> > Hex$(Ast.Adapt.adapter_address(ByteNum)), 2)
> >          Next

> >          If PhysicalAddress$ = "44:45:53:54:00:00" Then
> >             NumDialups = NumDialups + 1
> >             PhysicalAddress$ = "dialup" & Trim$(NumDialups)
> >          End If

> >          On Error GoTo AlreadyInList
> >          colPhysicalAddress.Add PhysicalAddress$, PhysicalAddress$
> >          On Error GoTo 0

> >          HeapFree GetProcessHeap(), 0, pAstat
> >       End If
> >    Next i
> >    Set colAdapterMacAddress = colPhysicalAddress
> >    Set colPhysicalAddress = Nothing
> >    Exit Sub

> > AlreadyInList:
> >    InList = True
> >    Resume Next
> > End Sub

> > '=====================================================
> > 'Module below is used to return a collection of Active Adapter Ip
>  Addresses
> > '=====================================================
> > Option Explicit

> > Private Const WS_VERSION_REQD = &H101
> > Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
> > Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
> > Private Const MIN_SOCKETS_REQD = 1
> > Private Const SOCKET_ERROR = -1
> > Private Const WSADescription_Len = 256
> > Private Const WSASYS_Status_Len = 128

> > Private Type hostent
> >    hName As Long
> >    hAliases As Long
> >    hAddrType As Integer
> >    hLength As Integer
> >    haddrlist As Long
> > End Type

> > Private Type WSADATA
> >    wVersion As Integer
> >    wHighVersion As Integer
> >    szDescription(0 To WSADescription_Len) As Byte
> >    szSystemStatus(0 To WSASYS_Status_Len) As Byte
> >    iMaxSockets As Integer
> >    iMaxUdpDg As Integer
> >    lpszVendorInfo As Long
> > End Type

> > Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
> > Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal
> > wVersionRequired As Integer, lpWSADATA As WSADATA) As Long
> > Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

> > Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal HostName$,
> > ByVal HostLen As Long) As Long
> > Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal HostName$)
> > As Long
> > Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal
> > hpvSource&, ByVal cbCopy&)

> > Function hibyte(ByVal wParam As Integer)
> >    hibyte = wParam \ &H100 And &HFF&
> > End Function

> > Function lobyte(ByVal wParam As Integer)
> >    lobyte = wParam And &HFF&
> > End Function

> > Sub SocketsInitialize()
> >    Dim WSAD As WSADATA
> >    Dim iReturn As Integer
> >    Dim sLowByte As String, sHighByte As String, sMsg As String

> >    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

> >    If iReturn <> 0 Then
> >       MsgBox "Winsock.dll is not responding."
> >       End
> >    End If

> >    If lobyte(WSAD.wVersion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wVersion) =
> > WS_VERSION_MAJOR And hibyte(WSAD.wVersion) <

...

read more »



Fri, 23 Apr 2004 11:08:19 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Ip and MAC address for each network adapter?

2. Network Adapter MAC address code snippet

3. Need to Retrieve Network Adapters MAC Address

4. Network adapter MAC address

5. Problems finding correct IP address using WINSOCK Control with 2 network adapters

6. Accessing the IP address of multiple network adapters using the MS Winsock Control

7. Accessinf the IP address of multiple network adapters using the MS Winsock Control

8. Problems finding correct IP address using WINSOCK Control with 2 network adapters

9. Accessing the IP address of multiple network adapters using the MS Winsock Control

10. Accessinf the IP address of multiple network adapters using the MS Winsock Control

11. Accessing the IP address of multiple network adapters using the MS Winsock Control

12. Accessinf the IP address of multiple network adapters using the MS Winsock Control

 

 
Powered by phpBB® Forum Software