根据域名获取IP地址

 
$PBExportHeader$n_cst_ip.sru
forward
global type n_cst_ip from nonvisualobject
end type
type s_wsadata from structure within n_cst_ip
end type
type os_hostent from structure within n_cst_ip
end type
end forward

type s_wsadata from structure
    unsignedinteger        version
    unsignedinteger         highversion 
    character        discription[257]
    character        systemstatus[129]
    unsignedinteger        maxsockets
    unsignedinteger        maxupddg
    string         vendorinfo
end type

type os_hostent from structure
    long        hname
    long        haliases
    integer        haddrtype
    integer        hlen
    long        haddrlist
end type

global type n_cst_ip from nonvisualobject autoinstantiate
end type

type prototypes
FUNCTION  int  WSAStartup(  uint  UIVersionRequested,  ref  s_WSAData  lpWSAData  )    library  "wsock32.dll" 
FUNCTION  int  WSACleanup()  library  "wsock32.dll"  
FUNCTION  int  gethostname  (  ref  blob  name,  int  namelen  )  library  "wsock32.dll"  
FUNCTION  uLong  gethostbyname(ref blob  sHost)  Library  "wsock32.dll"  
FUNCTION  uLong  RtlMoveMemory(ref  os_hostent  hpvDest,long  hpvSource,long  cbCopy)  Library  "kernel32.dll"  
FUNCTION  uLong  RtlMoveMemory(ref  uLong  hpvDest,long  hpvSource,long  cbCopy)  Library  "kernel32.dll" 
end prototypes

forward prototypes
public function boolean of_getdnsip (string as_dns, ref string as_ipadress[])
end prototypes

public function boolean of_getdnsip (string as_dns, ref string as_ipadress[]);//函  数:of_GetDNSIp()
//功  能:得到指定域名的IP
//参  数:string as_DNS //域名
//        ref  string as_ipadress[] //返回的ip数组
//返回值:Boolean True/False   
//修改人:yyoinge

string ls_IpAddress[]
int li_IP_Count
s_wsadata l_WSAData
ULong ll_addr, ll_IP, ll_ipaddr, ll_stringip  
int li_version = 257
blob{128} lb_HostName
ulong ll_addr_list
os_hostent lstr_hostent

if wsastartup(li_version, l_WSAData)  =  0 then
    lb_hostname = blob('www.baidu.com')
    ll_addr  =  gethostbyname(lb_HostName)
    RtlMoveMemory(lstr_hostent, ll_addr, 16)
    ll_addr_list  =  lstr_hostent.haddrlist
 
    li_IP_Count  =  0 
    Do While True  
        RtlMoveMemory(ll_IPAddr, ll_addr_list, 4  )
        If ll_IPAddr = 0 Then Exit  
            li_IP_Count = li_IP_Count + 1
            RtlMoveMemory(ll_StringIP,  ll_IPAddr, 1)
            ls_IpAddress[li_IP_Count] = string(ll_StringIP) + "."
            RtlMoveMemory(ll_StringIP, ll_IPAddr + 1, 1)
            ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP) + "."
            RtlMoveMemory(ll_StringIP, ll_IPAddr + 2, 1)
            ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP)+ "."
            RtlMoveMemory(ll_StringIP, ll_IPAddr + 3, 1)
            ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP)
            ll_addr_list = ll_addr_list + 4
    Loop
    WSACleanup() 
End if

as_ipadress = ls_IpAddress

return upperbound(as_ipadress) > 0 //取到IP则返回true
end function

on n_cst_ip.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_ip.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on


程序中这样调用:
C/C++ coden_cst_ip ln
string ls_dsn = 'www.baidu.com',ls[]
if ln.of_getdnsip(ls_dsn, ls) then
    long i
    for i = 1 to upperbound(ls)
        messagebox('提示', '域名【' + ls_dsn + '】对应的IP地址(' + string(i) + ')为:~r~n~r~n' + ls[i])
    next
end if

你可能感兴趣的:(String,function,character,library,destructor,structure)