Visual Basic Network Services

GetIpAddrTable: IP Address Table of the Local Machine
     
Posted:   Sunday February 11, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows 98, NT4 SP4+, Windows 2000, Windows XP or later
Author:   VBnet - Randy Birch
     

Related:  

gethostbyname: Determine Network Host Name and IP Address
gethostbyname: Resolve Host Name to IP Address

gethostbyaddr: Obtain Host Name from IP Address
IcmpSendEcho: Ping a Machine by Host Name
     
 Prerequisites
One of the operating systems listed under OS Restrictions above.

The GetIpAddrTable function retrieves the interface–to–IP address mapping table.

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1), and a listview (Listview1). The code creates the required columns. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const ERROR_SUCCESS As Long = 0

Private Type MIB_IPADDRROW
  dwAddr As Long        'IP address
  dwIndex As Long       'index of interface associated with this IP
  dwMask As Long        'subnet mask for the IP address
  dwBCastAddr As Long   'broadcast address (typically the IP
                        'with host portion set to either all
                        'zeros or all ones)
  dwReasmSize As Long   'reassembly size for received datagrams
  unused1 As Integer    'not currently used (but shown anyway)
  unused2 As Integer    'not currently used (but shown anyway)
End Type

Private Declare Function GetIpAddrTable Lib "iphlpapi.dll" _
  (ByRef ipAddrTable As Byte, _
   ByRef dwSize As Long, _
   ByVal bOrder As Long) As Long
   
Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (dst As Any, src As Any, ByVal bcount As Long)
  
Private Declare Function inet_ntoa Lib "wsock32" _
   (ByVal addr As Long) As Long

Private Declare Function lstrcpyA Lib "kernel32" _
  (ByVal RetVal As String, _
   ByVal Ptr As Long) As Long
                        
Private Declare Function lstrlenA Lib "kernel32" _
  (ByVal Ptr As Any) As Long
   

Private Sub Form_Load()

   With ListView1
      .View = lvwReport
      .ColumnHeaders.Add , , "Index"
      .ColumnHeaders.Add , , "IP Address"
      .ColumnHeaders.Add , , "Subnet Mask"
      .ColumnHeaders.Add , , "Broadcast Addr"
      .ColumnHeaders.Add , , "Reassembly"
      .ColumnHeaders.Add , , "unused1"
      .ColumnHeaders.Add , , "unused2"
   End With
   
End Sub

      
Public Function GetInetStrFromPtr(ByVal Address As Long) As String
  
   GetInetStrFromPtr = GetStrFromPtrA(inet_ntoa(Address))

End Function


Public Function GetStrFromPtrA(ByVal lpszA As Long) As String

   GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
   Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
   
End Function


Private Sub Command1_Click()

   Dim IPAddrRow As MIB_IPADDRROW
   Dim buff() As Byte
   Dim cbRequired As Long
   Dim nStructSize As Long
   Dim nRows As Long
   Dim cnt As Long
   Dim itmx As ListItem
   
   Call GetIpAddrTable(ByVal 0&, cbRequired, 1)

   If cbRequired > 0 Then
    
      ReDim buff(0 To cbRequired - 1) As Byte
      
      If GetIpAddrTable(buff(0), cbRequired, 1) = ERROR_SUCCESS Then
      
        'saves using LenB in the CopyMemory calls below
         nStructSize = LenB(IPAddrRow)
   
        'first 4 bytes is a long indicating the
        'number of entries in the table
         CopyMemory nRows, buff(0), 4
      
         For cnt = 1 To nRows
         
           'moving past the four bytes obtained
           'above, get one chunk of data and cast
           'into an IPAddrRow type
            CopyMemory IPAddrRow, buff(4 + (cnt - 1) * nStructSize), nStructSize
            
           'pass the results to the listview
            With IPAddrRow
                Set itmx = ListView1.ListItems.Add(, , GetInetStrFromPtr(.dwIndex))
                itmx.SubItems(1) = GetInetStrFromPtr(.dwAddr)
                itmx.SubItems(2) = GetInetStrFromPtr(.dwMask)
                itmx.SubItems(3) = GetInetStrFromPtr(.dwBCastAddr)
                itmx.SubItems(4) = GetInetStrFromPtr(.dwReasmSize)
                itmx.SubItems(5) = (.unused1)
                itmx.SubItems(6) = (.unused2)
            End With
            
          Next cnt
          
      End If
   End If

End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter