Hello. Here is the WSAAsyncGetHostByName() function to use with timeout. I tried to make the function as easy as possible. Hope this now works for you.
Global Const $AF_INET = 2
Global Const $tagHostent = "ptr h_name;ptr h_aliases;short h_addrtype;short h_length;ptr h_addr_list"
Global $WSAAsyncGetHostByName_lParam = 0
TCPStartup()
Local $sAddress = "..localmachine"
Global $Timeout = 1000 ; 1 second timeout
Global $sIP = _WSAAsyncGetHostByName_Timeout($sAddress, $Timeout)
If @error Then
MsgBox(0, $sAddress, "Error: " & @error)
Else
MsgBox(0, $sAddress, "IP address: " & $sIP)
EndIf
TCPShutdown()
Func _WSAAsyncGetHostByName_Timeout($sNodeName = "..localmachine", $iTimeout = 0, $hGui = Default, $wMsg = Default)
;funkey 2015.04.13
Local Const $WM_USER = 0x400
Local Const $MAXGETHOSTSTRUCT = 1024
Local $bGuiDelete = False
If $wMsg = Default Then $wMsg = $WM_USER + 0x99
If $hGui = Default Then
$hGui = GUICreate("_WSAAsyncGetHostByName_Timeout")
$bGuiDelete = True
EndIf
GUIRegisterMsg($wMsg, "_WSAAsyncGetHostByName_Callback")
Local $tBuf = DllStructCreate("char buffer[" & $MAXGETHOSTSTRUCT & "]")
Local $aRet = DllCall("ws2_32.dll", "HANDLE", "WSAAsyncGetHostByName", "HWND", $hGui, "UINT", $wMsg, "str", $sNodeName, "struct*", $tBuf, "int", $MAXGETHOSTSTRUCT)
If $aRet[0] == 0 Then Return SetError(1, 0, 0)
Local $IsInTime = False
Local $iTimeStart = TimerInit()
While 1
If $WSAAsyncGetHostByName_lParam <> 0 Then
$IsInTime = True
ExitLoop
EndIf
Sleep(10)
If $iTimeout > 0 And TimerDiff($iTimeStart) > $iTimeout Then ExitLoop
WEnd
If Not $IsInTime Then
DllCall("ws2_32.dll", "int", "WSACancelAsyncRequest", "HANDLE", $aRet[0])
$WSAAsyncGetHostByName_lParam = 0
Return SetError(2, 0, 0)
EndIf
Local $WSAGETASYNCERROR = BitShift($WSAAsyncGetHostByName_lParam, 16)
Local $WSAGETASYNCBUFLEN = BitAND($WSAAsyncGetHostByName_lParam, 0xFFFF)
$WSAAsyncGetHostByName_lParam = 0
If $WSAGETASYNCERROR Then Return SetError($WSAGETASYNCERROR, $WSAGETASYNCBUFLEN, 0)
Local $sIP, $iIP, $tIPs
Local $tHostent = DllStructCreate($tagHostent, DllStructGetPtr($tBuf))
Local $ptrList = DllStructGetData($tHostent, "h_addr_list")
If $ptrList = 0 Then Return SetError(3, 0, 0)
Local $tAddressList = DllStructCreate("ptr", $ptrList)
Local $firstAddr = DllStructGetData($tAddressList, 1)
If DllStructGetData($tHostent, "h_addrtype") = $AF_INET Then
$tIPs = DllStructCreate("UINT", DllStructGetData($tAddressList, 1))
$iIP = DllStructGetData($tIPs, 1)
If $iIP = 0 Then Return SetError(4, 0, 0)
$sIP = _inet_ntoa($iIP)
EndIf
Return SetError(0, 0, $sIP)
EndFunc ;==>_WSAAsyncGetHostByName_Timeout
Func _WSAAsyncGetHostByName_Callback($hWnd, $iMsgID, $wParam, $lParam)
$WSAAsyncGetHostByName_lParam = $lParam
EndFunc ;==>_WSAAsyncGetHostByName_Callback
Func _inet_ntoa($In_Addr) ;only IPv4 (deprecated) --> use _inet_ntop() instead
Local $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "long", $In_Addr)
Return $aRet[0]
EndFunc ;==>_inet_ntoa