Board logo

标题: PB10如何获得本操作系统的IP地址 [打印本页]

作者: liuxing    时间: 2005-5-22 16:07     标题: PB10如何获得本操作系统的IP地址

怎样使用PB10如何获得本操作系统的IP地址
作者: Nothing    时间: 2005-5-22 20:12

IP地址与操作系统无关吧

应当叫本机的IP地址
使用Winsock控件,用Winsock1.LocalIP即可得到本机的IP地址
作者: Nothing    时间: 2005-5-22 20:23

下面是使用API的
/******************************************************
主题:提取机器名或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

在使用上述代码时要定义一个structure s_wsadata
作者: kso    时间: 2005-9-19 17:37

[quote="Nothing"]下面是使用API的
/******************************************************
主题:提取机器名或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

在使用上述代码时要定义一个structure s_wsadata[/quote4]

window2000 sp4, pb9 下通不过,提示gethost参数错误,何故?
作者: kso    时间: 2005-9-19 19:04

达人帮忙解决一下......
作者: Nothing    时间: 2005-9-19 19:23

[quote]在使用上述代码时要定义一个structure s_wsadata [/quote:58c06017b2]

注意上面的这句话。
s_wsadata可能是个数据结构,你查看一下你的PBL里是否有这个数据结构.

需要新建立pbw和pbt然后migrate才可以正常使用.
migrate的时候还要将另外一个pbl包含进来!
作者: Breanna6998    时间: 2005-12-25 16:57     标题: What is next

You'll decide that your previous conclusions were wrong and change ways?
作者: ljw9128    时间: 2006-8-9 15:02

免费下载郭宝利编写的N本PB电子图书,如果需要更多的PB电子图书到http://www.study01job.com网站,本站有郭宝利编写的多本电子图书供大家免费下载,并且还有很多关于PB学习的文章.




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0