Func __TCPIpToName($sIp, $iOption = Default, $hDll_Ws2_32 = Default)
Local $vbinIP, $vaDllCall, $vptrHostent, $vHostent, $sHostnames, $vh_aliases, $i
Local $INADDR_NONE = 0xffffffff, $AF_INET = 2, $sSeperator = @CR
If $iOption = Default Then $iOption = 0
If $hDll_Ws2_32 = Default Then $hDll_Ws2_32 = "Ws2_32.dll"
$vaDllCall = DllCall($hDll_Ws2_32, "long", "inet_addr", "str", $sIp)
If @error Then Return SetError(1, 0, "") ; inet_addr DllCall Failed
$vbinIP = $vaDllCall[0]
If $vbinIP = $INADDR_NONE Then Return SetError(2, 0, "") ; inet_addr Failed
$vaDllCall = DllCall($hDll_Ws2_32, "ptr", "gethostbyaddr", "long*", $vbinIP, "int", 4, "int", $AF_INET)
If @error Then Return SetError(3, 0, "") ; gethostbyaddr DllCall Failed
$vptrHostent = $vaDllCall[0]
If $vptrHostent = 0 Then
$vaDllCall = DllCall($hDll_Ws2_32, "int", "WSAGetLastError")
If @error Then Return SetError(5, 0, "") ; gethostbyaddr Failed, WSAGetLastError Failed
Return SetError(4, $vaDllCall[0], "") ; gethostbyaddr Failed, WSAGetLastError = @Extended
EndIf
$vHostent = DllStructCreate("ptr;ptr;short;short;ptr", $vptrHostent)
$sHostnames = __TCPIpToName_szStringRead(DllStructGetData($vHostent, 1))
If @error Then Return SetError(6, 0, $sHostnames) ; strlen/sZStringRead Failed
If $iOption = 1 Then
$sHostnames &= $sSeperator
For $i = 0 To 63 ; up to 64 Aliases
$vh_aliases = DllStructCreate("ptr", DllStructGetData($vHostent, 2) + ($i * 4))
If DllStructGetData($vh_aliases, 1) = 0 Then ExitLoop ; Null Pointer
$sHostnames &= __TCPIpToName_szStringRead(DllStructGetData($vh_aliases, 1))
If @error Then
SetError(7) ; Error reading array
ExitLoop
EndIf
Next
Return StringSplit(StringStripWS($sHostnames, 2), @CR)
Else
Return $sHostnames
EndIf
EndFunc ;==>__TCPIpToName
Func __TCPIpToName_szStringRead($iszPtr, $iLen = -1, $hDll_msvcrt = "msvcrt.dll")
Local $aStrLen, $vszString
If $iszPtr < 1 Then Return "" ; Null Pointer
If $iLen < 0 Then
$aStrLen = DllCall($hDll_msvcrt, "int:cdecl", "strlen", "ptr", $iszPtr)
If @error Then Return SetError(1, 0, "") ; strlen Failed
$iLen = $aStrLen[0] + 1
EndIf
$vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
If @error Then Return SetError(2, 0, "")
Return SetError(0, $iLen, DllStructGetData($vszString, 1))
EndFunc ;==>__TCPIpToName_szStringRead