Attribute VB_Name = "WinSockHelper"
Option Explicit
 
Public 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
 
Public Declare Function getHostByAddr _
    Lib "Ws2_32.dll" Alias "gethostbyaddr" ( _
        ByRef addr As Long, _
        ByVal addrLen As Integer, _
        ByVal addrType As Integer _
    ) _
As Long
 
Public Declare Function inetAddr _
    Lib "Ws2_32.dll" Alias "inet_addr" ( _
        ByVal addr As String _
    ) _
As Long
 
Public Declare Sub RtlMoveMemory _
    Lib "kernel32" ( _
        hpvDest As Any, _
        ByVal hpvSource As Long, _
        ByVal cbCopy As Long _
    )
 
Public Declare Sub CopyMemoryToStr _
    Lib "kernel32" Alias "RtlMoveMemory" ( _
        ByVal result As String, _
        ByVal sourcePtr As Long, _
        ByVal Length As Long _
    )
 
Declare Function StrLenPtr _
    Lib "kernel32" Alias "lstrlenA" ( _
        ByVal Ptr As Long _
    ) _
As Long
 
Declare Function GetLastWSDllError _
    Lib "Ws2_32.dll" Alias "WSAGetLastError" () _
As Long
 
Public Function getHostName(hAddr As String) As String
    On Error GoTo Hell
    Dim hEnt As Hostent
    Dim tempIP As Long
    Dim hEntAddress As Long
    getHostName = "(unknown)"
    tempIP = inetAddr(hAddr)
    hEntAddress = getHostByAddr(tempIP, 4, 2)
    If (hEntAddress = 0) Then
        Err.Number = GetLastWSDllError
        Err.Description = "Cannot resolve IP-Address to Host-Name."
        GoTo Hell
    End If
    RtlMoveMemory hEnt, hEntAddress, LenB(hEnt)
    getHostName = PointerToString(hEnt.h_name)
    Exit Function
Hell:
    Form1.errorHandler "Error while getting Host-Name"
End Function
 
Private Function PointerToString(p As Long) As String
    On Error GoTo Hell
    Dim c As Long
    Dim res As String
    c = StrLenPtr(p)
    res = String$(c, 0)
    CopyMemoryToStr res, p, c
    PointerToString = res
    Exit Function
Hell:
    Form1.errorHandler "Error while getting Host-Name"
    PointerToString = "PointerToStringError"
End Function