1、定义一个名为s_wsadata的结构,如下:
unsignedinteger version unsignedinteger highversion character discription[257] character systemstatus[129] unsignedinteger maxsockets unsignedinteger maxupddg string vendorinfo
二、在Local External Funtions中作如下的声明:
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 IF wsastartup ( li_version, l_WSAData ) = 0 THEN IF gethostname ( ls_HostName, len(ls_HostName) ) < 0 THEN messagebox("GetHostName",WSAGetLastError()) ELSE GetHost(ls_HostName, lb_HostAddress) 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") END IF WSACleanup() ELSE messagebox("GetHostName",WSAGetLastError()) END IF messagebox('ls_hostname: '+ls_hostname,'ls_ipaddress: '+ls_ipaddress)
//函数名称: f_getip //调用方法: f_getip(ip_name) //参数说明: ip_name : 类型:integer 含义:确定返回的结果(1:机名和IP地址,2:机名,3:IP地址,4:mac地址) //返回结果: string :由ip_name决定的反回结果 //函数功能: 得到本机的名和IP地址 //设计者: 时间: //建立全局的结构s_wsadata //结构如下: //version unsignedinteger //highversion unsignedinteger //description[257] character //systemstatus[129] character //maxsockets unsignedinteger //maxupddg unsignedinteger //vendorinfo string // 声明gloabe extern function //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 " //Function Long GetMACAddress(Long lana,Ref Long addr[6]) Library "GetMAC.dll" //Function Long GetLanaID(Ref Long lana[254]) Library "GetMAC.dll" s_wsadata l_WSAData string ls_HostName = space(128) string ls_IpAddress int li_version = 257 blob{4} lb_hostaddress //建立一个消息,基本的 winsock 版本 版本号分为两部分,主版本号从版本号 IF wsastartup ( li_version, l_WSAData ) = 0 THEN // 通过结构传递版本信息,类元素告诉我们版本信息。 //messagebox( "Winsock Version ", l_WSAData.description ) //获得当前主机的主机名称 IF gethostname ( ls_HostName, len(ls_HostName) ) < 0 THEN messagebox( "GetHostName ",WSAGetLastError()) ELSE //通过主机名,调用dll函数取得ip_address的指针给pb blob 变量,长度是4个字节。因为内部结构包括4个指 //针,每一个指针指向ip_address的一部分,总共是4个字节 // Messagebox( "Hostname ", ls_HostName) GetHost(ls_HostName, lb_HostAddress) //改变指针的获得ip_address构造ip地址 27pbws32.dll 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 //清除所建创的信息 WSACleanup() ELSE messagebox( "GetHostName ",WSAGetLastError()) END IF long ll_mac[6] long ll_lana[254],ll_lananum integer ll_nub string ls_mac long ll_numb ll_lananum = GetLanaID(ll_lana) for ll_nub=1 to ll_lananum if GetMacAddress(ll_lana[ll_nub],ll_mac) = 0 then ls_mac=string(string(f_change_jz(ll_mac[1],16))+ '- '+& MessageBox( "LANA " + string(ll_lana[ll_nub]),& string(ll_mac[1])+ ", " + & string(ll_mac[2])+ ", " + & string(ll_mac[3])+ ", " + & string(ll_mac[4])+ ", " + & string(ll_mac[5])+ ", " + & string(ll_mac[6])) end if next choose case ip_name case 1 return "本机名 "+ ls_hostname+ " IP "+ls_ipaddress+ " mac "+ls_mac case 2 return "本机名: "+ls_hostname case 3 return "IP地址: "+ls_ipaddress case 4 return "MAC地址: "+ls_mac end choose