读书人

PB10下怎么获取本机IP地址? (100分,解

发布时间: 2012-05-23 13:44:13 作者: rapoo

PB10下如何获取本机IP地址? (100分,解决马上结帖)
RT

[解决办法]
直接取注册表
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
[解决办法]
楼上估计对自动获取的无效吧?
run( 'ipconfig /all > c:\tmp.text ')
然后读c:\tmp.text吧……
[解决办法]
/*************************************************************************

函数:string gf_ip()

功能:获取本地的IP,若本地有多个IP,侧用“,”分隔

返回数据格式:192.168.000.134,192.168.000.136,...

日期:2007.1.12

**************************************************************************/
string ls_IpAddress[]
int li_IP_cnt
string ls_ipmac
str_wsadata l_WSAData
string ls_HostName
ULong ll_addr,ll_IP,ll_ipaddr,ll_stringip
int li_version = 257
int li_i,li_j
blob{128} lb_HostName
ulong ll_addr_list
str_hostent lstr_hostent

ls_ipmac = " "
if wsastartup ( li_version, l_WSAData ) = 0 then
//初始化Windows Sockets 成功
IF gethostname ( lb_HostName, 128 ) < 0 THEN
//获取主机名失败
ELSE
li_j = lena(lb_hostname)
ls_hostname = " "
for li_i = 1 to li_j
ls_hostname = ls_hostname + string(BlobMid(lb_hostname,li_i, 1),EncodingANSI!)
next
ls_HostName = trim(ls_HostName)//主机名
ll_addr = gethostbyname(lb_HostName)//获取指向IP地址的指针地址
RtlMoveMemory ( lstr_hostent, ll_addr, 16 )//提取指向IP地址的指针结构
ll_addr_list = lstr_hostent.h_Addrlist // 取得指向IP地址的指针, ll_addr_list指向一个指向IP地址的指针数组

li_IP_cnt = 0//计数IP个数
Do While True
RtlMoveMemory ( ll_IPAddr , ll_addr_list, 4 ) // 取得指向IP地址的指针值
If ll_IPAddr = 0 Then Exit
li_IP_cnt = li_IP_cnt + 1
//分段获取IP值
RtlMoveMemory (ll_StringIP, ll_IPAddr, 1 )
ls_IpAddress[li_IP_cnt] = right( "000 " + string(ll_StringIP),3) + ". "
RtlMoveMemory (ll_StringIP, ll_IPAddr + 1, 1 )
ls_IpAddress[li_IP_cnt] = ls_IpAddress[li_IP_cnt] + right( "000 " + string(ll_StringIP),3) + ". "
RtlMoveMemory (ll_StringIP, ll_IPAddr + 2, 1 )
ls_IpAddress[li_IP_cnt] = ls_IpAddress[li_IP_cnt] + right( "000 " + string(ll_StringIP),3) + ". "
RtlMoveMemory (ll_StringIP, ll_IPAddr + 3, 1 )
ls_IpAddress[li_IP_cnt] = ls_IpAddress[li_IP_cnt] + right( "000 " + string(ll_StringIP),3)

ll_addr_list = ll_addr_list + 4 // 指针加4,继续读取下一个IP地址
Loop
END IF
WSACleanup()
end if
or li_i = 1 to li_IP_cnt
ls_ipmac = ls_ipmac + ls_IpAddress[li_i] + ", "
next
if(len(ls_ipmac) > 1)then ls_ipmac = left(ls_ipmac,len(ls_ipmac) - 1)
return ls_ipmac
[解决办法]
获取IP地址

利用API函数:
定义下列外部函数
FUNCTION ulong gethostbyname ( String name) LIBRARY "wsock32.dll "
FUNCTION Long gethostname ( REF String name, Long namelen) LIBRARY "wsock32.dll "
FUNCTION uLong RtlMoveMemory(ref hostent hpvDest,long hpvSource,long cbCopy) Library "kernel32.dll "
FUNCTION uLong RtlMoveMemory(ref Long hpvDest,long hpvSource,long cbCopy) Library "kernel32.dll "
FUNCTION uLong RtlMoveMemory(ref ipaddress hpvDest,long hpvSource,long cbCopy) Library "kernel32.dll "

代码如下:
string ls_hostname = space(255),ls_ip= ' '
long ll_ip
hostent lhst_host
long ll_dwIPAddr
int li_loop = 1
ipaddress tmpipaddr
if gethostname(ls_hostname,255) = -1 then
messagebox( '错误 ', '获取主机名出错! ')
return '-1 '


end if
ls_hostname = trim(ls_hostname)
ll_ip = gethostbyname(ls_hostname)
if ll_ip = 0 then
messagebox( '错误 ', '获取ip地址出错! ')
return '-1 '
end if
RtlMoveMemory(lhst_host,ll_ip,16)
RtlMoveMemory(ll_dwIPAddr,lhst_host.haddrlist,4)
do while ll_dwIPAddr <> 0
RtlMoveMemory(tmpipaddr,ll_dwIPAddr,4)
if li_loop = 1 then
ls_ip = ls_ip + string(asc(tmpipaddr.addr1)) + '. '+ string(asc(tmpipaddr.addr2)) + '. '+ string(asc

(tmpipaddr.addr3)) + '. '+ string(asc(tmpipaddr.addr4))
else
ls_ip = ls_ip + ', ' + string(asc(tmpipaddr.addr1)) + '. '+ string(asc(tmpipaddr.addr2)) + '. '+ string(asc

(tmpipaddr.addr3)) + '. '+ string(asc(tmpipaddr.addr4))
end if
RtlMoveMemory(ll_dwIPAddr,lhst_host.haddrlist+li_loop*4,4)
li_loop ++
loop
return ls_ip
这是个获取本机IP地址的函数,返回值为string,这里ls_ip就是本机的ip地址,如有多个ip则是用 ', '来间隔的。

读书人网 >PB

热点推荐