Performs a lookup of a hostname and returns its IP address. If the lookup fails the function returns a blank string.

You must first call WSAStartup() to initialize the Winsock DLL prior to doing the lookup, and call WSACleanup() when operations are complete.

Language: 
Dark Basic Pro
Code: 
` Loads the Winsock DLL
function WSAStartup()
   load dll "Ws2_32.dll", 1
   ptr = make memory(400)
   result = call dll(1, "WSAStartup", 2, ptr)
   delete memory ptr
endfunction result

` Unloads the Winsock DLL
function WSACleanup()
   call dll 1, "WSACleanup"
   delete dll 1
endfunction

` Looks up the IP address for the specified web address
function GetHostByAddr(host as string)
   hostent as dword
   ptr as dword
   ip as dword

   hostent = call dll(1, "gethostbyname", host)
   if hostent = 0 then exitfunction ""
   ptr = hostent + 12
   ptr = *ptr
   ptr = *ptr
   ip = *ptr
   result$ = str$(ip && 0xFF) + "." + str$((ip && 0xFF00) >> 8) + "." + str$((ip && 0xFF0000) >> 16) + "." + str$((ip && 0xFF000000) >> 24)
endfunction result$
5
Average: 5 (2 votes)