|
|
|
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
Type IPINFO dwAddr As Long dwIndex As Long dwMask As Long dwBCastAddr As Long dwReasmSize As Long unused1 As Integer unused2 As Integer End Type
Type MIB_IPADDRTABLE dEntrys As Long mIPInfo(MAX_IP) As IPINFO 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
Type IP_EntryInfo IPAddress As String SunetMask As String BroadcastAddress As String End Type
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
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
On Error GoTo errHandler GetIpAddrTable ByVal 0&, Ret, True
If Ret <= 0 Then Exit Function ReDim bBytes(0 To Ret - 1) As Byte GetIpAddrTable bBytes(0), Ret, False
CopyMemory Listing.dEntrys, bBytes(0), 4
For Tel = 0 To Listing.dEntrys - 1 ReDim Preserve arrReturnIPinfo(0 To Tel) 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
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 ) |
|
|