10-08-2008, 06:03 PM
Function GetIpAddress2
This function will be in next QM version.
;/
function# $computer str&ip [flags] ;;flags: 1 get all
;Retrieves IP address of computer.
;Returns 1 on success, 0 on failure.
;For local computer name, use "".
;If flag 1 used, gets list of IP addresses of the computer.
;computer can be like computer:port. Then IP will be like ip:port.
;EXAMPLE
;str ip
;if(GetIpAddress2("PC-3" ip))
,;out ip
if(!GetEnvVar("qm_wsa" _s) or !_s.len)
,SetEnvVar "qm_wsa" 1
,WSADATA wsaData
,WSAStartup(0x0002 &wsaData)
ip=""
;if computer:port, separate, and later append port to IP
str cn port
int nt=tok(computer &cn 2 ":")
if(nt>1) computer=cn
if(empty(computer)) computer=0
hostent* hp = gethostbyname(computer); if(!hp) ret
int i
for i 0 1000
,if(hp.h_addr_list[i] && hp.h_length>=4) else break
,byte* b=hp.h_addr_list[i]
,int* ipi=b; if(*ipi=0x100007F) continue ;;"127.0.0.1"
,if(i) ip+"[]"
,ip.formata("%u.%u.%u.%u" b[0] b[1] b[2] b[3])
,if(nt>1) ip.formata(":%s" port)
,if(flags&1=0) break
ret ip.len!=0
This function will be in next QM version.