Thomas Risi Softwareentwicklung
Addins - Datenbanklösungen - Komponenten - RTDServer - WebServices
GetHostIP
Aus einer Frage in einem Forum ...
Es soll die IPv4-Adresse eine Netzwerkteilnehmers ermittelt werden. Und das als benutzerdefinierte Tabellenfunktion in Excel.
Quellcode für ein normales Modul, z.B. Modul1 ...
Option Explicit
Private Declare Function GetHostByName Lib "Ws2_32.dll" Alias "gethostbyname" ( _
ByVal hostname As String) As Long
Private Declare Function WSAStartup Lib "Ws2_32.dll" ( _
ByVal wVersionRequired&, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "Ws2_32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal Length As Long)
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUDPDG As Integer
lpVendorInfo As Long
End Type
Private Type Hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Const WSA_SUCCESS As Long = 0
Private Const WS_VERSION_REQD As Long = &H101
Public Function GetHostIP(ByVal hostname As String) As String
Dim host As Long
Dim hostinfo As Hostent
Dim ip As Long
Dim ips(3) As Byte
Dim WSAD As WSADATA
GetHostIP = "0.0.0.0"
If Left$(hostname, 7) = "http://" Then _
hostname = Mid(hostname, 8)
If Left$(hostname, 8) = "https://" Then _
hostname = Mid(hostname, 9)
If WSAStartup(WS_VERSION_REQD, WSAD) = WSA_SUCCESS Then
host = GetHostByName(hostname)
If host <> 0 Then
CopyMemory hostinfo, ByVal host, Len(hostinfo)
CopyMemory ip, ByVal hostinfo.h_addr_list, 4
CopyMemory ips(0), ByVal ip, 4
GetHostIP = ips(0) & "." & ips(1) & "." & ips(2) & "." & ips(3)
End If
WSACleanup
End If
End Function
© 2001 -
by Thomas Risi