Option Explicit
Const MAX_IP = 5
Type IPINFO
dwAddr As Long ' Get IP address
dwIndex As Long
dwMask As Long ' subnet mask
dwBCastAddr As Long ' broadcast address
dwReasmSize As Long ' assembly size
unused1 As Integer
unused2 As Integer
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
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
'Private Declare Function GetIpAddrTable Lib "IPHLPAPI.dll" (ByRef pIpAddrTable As MIB_IPADDRTABLE, ByRef pdwSize As Long, ByVal border As Long) As Long
Private Declare Function GetIpAddrTable Lib "IPHLPAPI.dll" (ByRef pIpAddrTable As Byte, ByRef pdwSize As Long, ByVal border As Long) As Long
Public Function IPAddress() As Variant
'******************************************************************************
'* *
'* Name: IPAddress *
'* *
'* Purpose: Get IPAddress *
'* *
'* Returns: IPAddress *
'* *
'******************************************************************************
On Error GoTo PROC_ERROR
Dim ret As Long, i As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE
Dim strIP As String, strTemp As String
Dim TempArr() As String
Dim IPCount As Long
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 i = 0 To Listing.dEntrys - 1
CopyMemory Listing.mIPInfo(i), bBytes(4 + (i * Len(Listing.mIPInfo(i)))), Len(Listing.mIPInfo(i))
strTemp = ConvertAddressToString(Listing.mIPInfo(i).dwAddr)
If strTemp <> "0.0.0.0" Then
IPCount = IPCount + 1
'strIP = strIP & IIf(Len(strIP) = 0, "", ";") & strTemp
ReDim Preserve TempArr(IPCount - 1) As String
TempArr(IPCount - 1) = strTemp
End If
'//strIPSubNetMask = "IP Subnetmask : " & ConvertAddressToString(Listing.mIPInfo(i).dwMask)
'//strBroadCastIPAddress = "BroadCast IP address : " & ConvertAddressToString(Listing.mIPInfo(i).dwBCastAddr)
Next
'IPAddress = strIP
[COLOR="Red"] 'Return to array of IP
'On the Excel Worksheet, select cells in the same row, enter formula =IPAddress() , to get the array values, press CTRL+SHIFT+ENTER (combination).
'If you want to get the first IP, enter formula =IPAddress() then pressing ENTER key only.[/COLOR]
IPAddress = TempArr
PROC_DONE:
Exit Function
PROC_ERROR:
'Call Process_Error(MODULE_NAME, "IPAddress")
Resume PROC_DONE
End Function
Public Function ConvertAddressToString(longAddr As Long) As String
Dim myByte(3) As Byte
Dim Cnt As Long
CopyMemory myByte(0), longAddr, 4
For Cnt = 0 To 3
ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) & "."
Next Cnt
ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function