Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


This article will show you how to get all installed IP addresses and subnet mask on your system.

To implement quick demo perform the following steps

- Create a standard exe project
- Add a module
- Add 1 command button and 1 text box with MultiLine=True

Place the following code in Form1 declaration

Form1.frm

Click here to copy the following block
Private Sub Command1_Click()
  Dim arrIpInfo() As IP_EntryInfo
  Ret = GetInstalledIPInfo(arrIpInfo)
  If Ret > 0 Then
    Text1 = Text1 & Ret & " IP(s) Detected on your system" & vbCrLf
    Text1 = Text1 & String(32, "-") & vbCrLf
    For i = 0 To Ret - 1
      Text1 = Text1 & "IP Address    : " & arrIpInfo(i).IPAddress & vbCrLf
      Text1 = Text1 & "Subnet Mask    : " & arrIpInfo(i).SunetMask & vbCrLf
      Text1 = Text1 & "Broadcase Address : " & arrIpInfo(i).BroadcastAddress & vbCrLf
      Text1 = Text1 & String(32, "=") & vbCrLf
    Next
  End If
End Sub

And place the following code in Module1

Module1.bas

Click here to copy the following block
Const MAX_IP = 20  'To make a buffer... (For maximum 20 ips on your pc..)

Type IPINFO
  dwAddr As Long  ' IP address
  dwIndex As Long  ' interface index
  dwMask As Long  ' subnet mask
  dwBCastAddr As Long  ' broadcast address
  dwReasmSize As Long  ' assembly size
  unused1 As Integer  ' not currently used
  unused2 As Integer  '; not currently used
End Type

Type MIB_IPADDRTABLE
  dEntrys As Long  'number of entries in the table
  mIPInfo(MAX_IP) As IPINFO  'array of IP address entries
End Type

Type IP_Array
  mBuffer As MIB_IPADDRTABLE
  BufferLen As Long
End Type

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long

'//User Defined Type to store only required info for IP entry

Type IP_EntryInfo
  IPAddress As String
  SunetMask As String
  BroadcastAddress As String
End Type

'converts a Long to a string
Function ConvertIPToString(longAddr As Long) As String
  Dim myByte(3) As Byte
  Dim Cnt As Long
  CopyMemory myByte(0), longAddr, 4
  For Cnt = 0 To 3
    ConvertIPToString = ConvertIPToString + CStr(myByte(Cnt)) + "."
  Next Cnt
  ConvertIPToString = Left$(ConvertIPToString, Len(ConvertIPToString) - 1)
End Function
'//////////////////////////////////////////////////////////
'//Input : array variable of Type IP_EntryInfo
'//Returns : Count of Number of installed IPs
'//     and it fills arrReturnIPinfo with IP info
'//////////////////////////////////////////////////////////
Function GetInstalledIPInfo(ByRef arrReturnIPinfo() As IP_EntryInfo) As Integer
  Dim Ret As Long, Tel As Long
  Dim bBytes() As Byte
  Dim Listing As MIB_IPADDRTABLE

  'Dim arrIPEntries() As IP_EntryInfo '//to store all IP information

  On Error GoTo errHandler
  GetIpAddrTable ByVal 0&, Ret, True

  If Ret <= 0 Then Exit Function
  ReDim bBytes(0 To Ret - 1) As Byte
  'retrieve the data
  GetIpAddrTable bBytes(0), Ret, False

  'Get the first 4 bytes to get the entry's.. ip installed
  CopyMemory Listing.dEntrys, bBytes(0), 4

  For Tel = 0 To Listing.dEntrys - 1
    ReDim Preserve arrReturnIPinfo(0 To Tel)
    'Copy whole structure to Listing..
    CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))

    arrReturnIPinfo(Tel).IPAddress = ConvertIPToString(Listing.mIPInfo(Tel).dwAddr)
    arrReturnIPinfo(Tel).SunetMask = ConvertIPToString(Listing.mIPInfo(Tel).dwMask)
    arrReturnIPinfo(Tel).BroadcastAddress = ConvertIPToString(Listing.mIPInfo(Tel).dwBCastAddr)
  Next

  GetInstalledIPInfo = Listing.dEntrys

  'MsgBox ConvertIPToString(Listing.mIPInfo(1).dwAddr)
  Exit Function
errHandler:
  MsgBox Err.Description
End Function



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.