Enumerating IPs
From HashVB
This article is based on Visual Basic 6. Find other Visual Basic 6 articles. |
If you need to know all the IP addresses allocated to the local machine, there is a special procedure to go through as follows:
- Get the local host name
- Get the host information for the local host name
- Read all the listed IPs
Unfortunately, this involves the Hostent structure which is very hard to deal with in VB.
Various API calls and structures are needed for this function:
Private Type WSAData wVersion As Integer wHighVersion As Integer szDescription(0 To 256) As Byte 'WSADESCRIPTION_LEN szSystemStatus(0 To 128) As Byte 'WSASYS_STATUS_LEN wMaxSockets As Integer wMaxUDPDG As Integer dwVendorInfo As Long End Type Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLen As Integer hAddrList As Long End Type Private Declare Function WSAStartup Lib "WSOCK32" (ByVal wVersionRequired As Long, _ lpWSADATA As WSAData) As Long Private Declare Function WSACleanup Lib "WSOCK32" () As Long Private Declare Function gethostname Lib "WSOCK32" (ByVal szHost As String, _ ByVal dwHostLen As Long) As Long Private Declare Function gethostbyname Lib "WSOCK32" (ByVal szHost As String) As Long Private Declare Function inet_ntoa Lib "ws2_32.dll" (ByVal laddr As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, _ ByVal ByteLen As Long) Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, _ lpString2 As Any) As Long
And now the function itself:
Public Function EnumurateIPs(IPs() As String) As Long Dim WSData As WSAData Dim HostName As String Dim HostInfoPointer As Long Dim HostInfo As HOSTENT Dim IPPointer As Long Dim IPLong As Long Dim IPAddressP As Long Dim IPAddress As String Dim Count As Long 'Initialise everything Count = 0 IPs = Split("", "") If WSAStartup(&H101, WSData) <> 0 Then Exit Function 'Get the local host name HostName = String(256, vbNullChar) If gethostname(HostName, Len(HostName)) <> -1 Then If InStr(HostName, vbNullChar) > 0 Then HostName = Left(HostName, InStr(HostName, vbNullChar) - 1) 'Lookup the information for the local host HostInfoPointer = gethostbyname(HostName) If HostInfoPointer <> 0 Then 'Copy it into a nice hostent structure CopyMemory HostInfo, ByVal HostInfoPointer, Len(HostInfo) Do 'Copy the IP address pointer out of the "array" CopyMemory IPPointer, ByVal HostInfo.hAddrList + (4 * Count), 4 'Loop until we find a null item If IPPointer = 0 Then Exit Do 'Copy the IP itself into a variable CopyMemory IPLong, ByVal IPPointer, HostInfo.hLen 'Convert the pointer to an IP string IPAddressP = inet_ntoa(IPLong) IPAddress = String(lstrlen(ByVal IPAddressP), vbNullChar) Call lstrcpy(IPAddress, ByVal IPAddressP) 'Add it to the list ReDim Preserve IPs(0 To Count) IPs(Count) = IPAddress Count = Count + 1 Loop End If End If 'Clean up WSACleanup EnumurateIPs = Count End Function