如何取得本機的IP

出自VFP Wiki

跳轉到: 導航, 搜尋


來源:

? GetIp("www.AtoutFox.org")
? GetIp("www.foxforum.net")
? GetIp("www.foxprofr.com")
? GetIp("news.atoutfox.org")
? GetIp("xfrx.fr")
? GetIp("www.microsoft.com")
? GetIp("Fox.wikis.com")
* -------------
Function GetIP(p_host)
local v_Struct, v_Buffer, v_IP, v_r, v_i
p_host = Alltrim(p_host)
v_r = ""
#DEFINE HOSTENT_SIZE 16
DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING lpWSAData
if WSAStartup(0x202, Repli(Chr(0),512)) <> 0
v_r = "0.0.0.0, 無法初使化 WinSock (WS2_32.DLL) 函式庫."
wait windows v_r
else
DECLARE INTEGER gethostbyname IN ws2_32 STRING host

v_Struct = gethostbyname(p_host)

if v_Struct = 0
v_r = "0.0.0.0, host : "+p_host+" 無法解析 IP 位址"
wait windows v_r
else
v_Buffer = Repli(Chr(0), HOSTENT_SIZE)
v_IP = Chr(0) + Chr(0) + Chr(0) +Chr(0)
DECLARE RtlMoveMemory IN kernel32 STRING @Destination, INTEGER Source, INTEGER Longueur
=RtlMoveMemory(@v_Buffer, v_Struct, HOSTENT_SIZE)
=RtlMoveMemory(@v_ip, b2dword(SubStr(v_Buffer,13,4)), 4)
=RtlMoveMemory(@v_ip, b2dword(v_ip), 4)
for v_i=1 to 4
v_r = v_r + Alltrim(str(Asc(Substr(v_ip,v_i,1)),3,0))+ Iif(v_i<4,".","")
next
clear dlls RtlMoveMemory
endif
DECLARE INTEGER WSACleanup IN ws2_32
=WSACleanup()
clear dlls WSACleanup
clear dlls gethostbyname
endif
clear dlls WSAStartup
return v_r
* -------------
FUNCTION b2dword(p_buffer)
RETURN Asc(substr(p_buffer, 1,1)) + Bitlshift(Asc(substr(p_buffer, 2,1)),8) + Bitlshift(Asc(substr(p_buffer, 3,1)),16) + Bitlshift(Asc(substr(p_buffer, 4,1)),24)
* -------------