当前位置: 代码迷 >> PB >> 求简单的稳定的获取本机ip的方法,该如何解决
  详细解决方案

求简单的稳定的获取本机ip的方法,该如何解决

热度:19   发布时间:2016-04-29 08:48:39.0
求简单的稳定的获取本机ip的方法
附代码最好,谢谢

------解决方案--------------------
/****************************************************** 
主题:提取机器名或IP地址 
入参:name_ip 1为机器名、非1IP 
出参:机器名或IP 
调用: 
function int WSAStartup( uint UIVersionRequested, ref s_WSAData lpWSAData ) library "wsock32.dll " 
function int WSACleanup() library "wsock32.dll " 
function int WSAGetLastError ( ) library "wsock32.dll " 
function int gethostname ( ref string name, int namelen ) library "wsock32.dll " 
function string GetHost(string lpszhost, ref blob lpszaddress ) library "pbws32.dll "
*******************************************************/ 

s_wsadata l_WSAData 
string ls_HostName = space(128) 
string ls_IpAddress 
int li_version = 257 
blob{4} lb_hostaddress
/* Then, create a session, based on the winsock version. This version number consists of two part, a major and minor release number, both represented in a byte. So, version 1.1 gives us an integer version of 257 ( 256 + 1 ) */ 
IF wsastartup ( li_version, l_WSAData ) = 0 THEN 
/* the wsadata structure contains several information. The description element tells us the winsock version */ 
//messagebox( "Winsock Version ", l_WSAData.description ) 
/* Now, let 's find out what the hostname is of the current machine we 're working on */ 
IF gethostname ( ls_HostName, len(ls_HostName) ) < 0 THEN 
messagebox( "GetHostName ",WSAGetLastError()) 
ELSE 
/* With the hostname, call the DLL function and map the IP-address pointers to a PB blob variable, with a length of 4 bytes. This is done because the internal structure contains 4 pointers, each pointer point to one of the parts of the IP-address. An IP-address namely, consists of 4 bytes */ 
// Messagebox( "Hostname ", ls_HostName) 
GetHost(ls_HostName, lb_HostAddress) 
/* Convert the pointers to scalars, and concatenate them to one string, the IP-address */ 
ls_IpAddress = string(asc(string(blobmid(lb_HostAddress,1,1))), "000 ") + ". " 
ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,2,1))), "000 ") + ". " 
ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,3,1))), "000 ") + ". " 
ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,4,1))), "000 ") 
//Messagebox( "Ip Address ", ls_IpAddress ) 
END IF 
/* We 're finished, clean up the mess we made */ 
WSACleanup() 
ELSE 
messagebox( "GetHostName ",WSAGetLastError()) 
END IF
if name_ip=1 then 
return ls_hostname 
else 
return ls_ipaddress 
end if


http://topic.csdn.net/t/20020711/09/865435.html
------解决方案--------------------
将以下内容保存为本地文件n_cst_ip.sru
然后导入pbl中
C/C++ code
$PBExportHeader$n_cst_ip.sruforwardglobal type n_cst_ip from nonvisualobjectend typetype s_wsadata from structure within n_cst_ipend typetype os_hostent from structure within n_cst_ipend typeend forwardtype s_wsadata from structure    unsignedinteger        version    unsignedinteger         highversion     character        discription[257]    character        systemstatus[129]    unsignedinteger        maxsockets    unsignedinteger        maxupddg    string         vendorinfoend typetype os_hostent from structure    long        hname    long        haliases    integer        haddrtype    integer        hlen    long        haddrlistend typeglobal type n_cst_ip from nonvisualobject autoinstantiateend typetype prototypesFUNCTION  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 prototypesforward prototypespublic function boolean of_getlocaip (ref string as_ipadress[])end prototypespublic function boolean of_getlocaip (ref string as_ipadress[]);//函  数:of_getlocaip()//功  能:得到本机IP//参  数:ref  string as_ipadress[] //ip数组//返回值:Boolean True/False   //创建人:李振军 2008.12.9//修改人:string ls_IpAddress[]int li_IP_Countstring ls_ipmacs_wsadata l_WSADatastring ls_HostNameULong ll_addr, ll_IP, ll_ipaddr, ll_stringip  int li_version = 257int li_i, li_jblob{128} lb_HostNameulong ll_addr_listos_hostent lstr_hostent ls_ipmac = ""if wsastartup(li_version, l_WSAData)  =  0 then            IF gethostname(lb_HostName, 128) < 0 THEN        //获取主机名失败        Return False    ELSE        li_j = len(lb_hostname)        ls_hostname = ""        For li_i = 1 To li_j            ls_hostname  =  ls_hostname + string(BlobMid(lb_hostname,  li_i,  1))        Next        ls_HostName  =  trim(ls_HostName)//主机名        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    End if        WSACleanup() End ifFor li_i = 1 To li_IP_Count     as_ipadress[li_i] = ls_IpAddress[li_i]Nextreturn Trueend functionon n_cst_ip.createcall super::createTriggerEvent( this, "constructor" )end onon n_cst_ip.destroyTriggerEvent( this, "destructor" )call super::destroyend on
  相关解决方案