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$
(2 votes)
Sat, 06/09/2007 - 00:28
And very thorough too - it even handles a failed lookup.
Question - why do you add 12 to hostent?
Sat, 06/09/2007 - 16:01
Well you see hostent holds a memory address, so I have to add 12 to get the address of the necessary item in the hostent structure, which is a pointer to a list of pointers to unsigned integer values (the IP addresses) - or something like that.