PB + API取局域网计算机名、IP、MAC、工作组等信息

PB + API取局域网计算机名、IP、MAC、工作组等信息  

原理是用 NBTSTAT命令取得相关信息 如 (nbtstat -a 机器名 >存放文件名.txt ) ,然后再从 txt中出相关内容,此方法感觉 最好,之前有用网上的getmac.dll获取,但如果是无线的网卡可能取不出,mac信息.

  转载如下:

网上一些取局域网所有计算机信息的编程基本都用到一些DLL,有的例程只能取2000/XP的计算机名。这里我提供一种方法,纯粹使用PB + API来实现取局域网一个网段的所有计算机的信息(使用了网上下载的《PB扩充函数1.5》)。而且,按照此方法,我们可以取得任何DOS命令能取得的信息。

      使用的API

Function boolean IsWindow (Long hwnd ) Library “user32.dll”

FUNCTION ulong WinExec(ref string lpCmdLine,ulong nCmdShow) LIBRARY “kernel32.dll”

       使用到的《PB扩充函数1.5》中的函数

       uf_Network_Pinguf_file_isavailable。虽然使用《PB扩充函数1.5》时需要一个mhand.dll,但是我们用到的2个函数并没有使用到mhand.dll,所以也算是没有借助第三方DLL

       检索IP等信息使用2个自建的函数:

f_searchip():

string ls_ip,ls_temp

//ls_temp为需要检索的ip段,格式为xxx.xxx.xxx. 如:1921680

ls_temp=192.168.0.

for i=1 to 255

       ls_ip=ls_temp + string(i)

       f_searchip1(ls_ip)

next

f_searchip1(string ls_ip):

//得到一个一个ip地址计算机信息并且插入数据窗口

u_kchs        lu_kchs  

string        ls_temp

long              ll_row,p

integer        li_filenum

ulong        ll_handle

string        ls_pcname,ls_mac,ls_group

sle_ts.text=’正在检索‘+as_ip

//如果能ping通为有效ip

if not(lu_kchs.uf_Network_Ping(as_ip)) then return

//使用NBTSTAT命令取得相关信息

ls_temp=”nbtstat -a “+as_ip+”>temp\”+as_ip

li_FileNum = FileOpen(“run.bat”,StreamMode!, Write!, LockWrite!, Replace!)

FileWrite(li_FileNum,ls_temp)

FileClose(li_filenum)

ls_temp=’run.bat’

ll_handle=WinExec(ls_temp,0)

//等待DOS窗口关闭

Do While isWindow(ll_handle)

       Yield()

Loop

//等待临时文件创建成功

do while not(lu_kchs.uf_file_isavailable(“temp\”+as_ip))

       Yield()

Loop

//取计算机mac,工作组等信息

li_FileNum=FileOpen(“temp\”+as_ip,StreamMode!,Read! )

if li_FileNum>0 then

       FileRead(li_FileNum,ls_temp)

       FileClose(li_filenum)

       FileDelete(“temp\”+as_ip)

              

       p=pos(ls_temp,’MAC Address = ‘)

       ls_mac=mid(ls_temp,p + 14,17)

              

       p=pos(ls_temp,’UNIQUE      Registered’)

       ls_pcname=trim(mid(ls_temp,p – 21,14))

              

       p=pos(ls_temp,’GROUP       Registered’)

       ls_group=trim(mid(ls_temp,p – 21,14))

      

       if ls_mac=’\NetBT_Tcpip_{942′ then ls_mac=’其他设备

       if ls_mac<>’其他设备‘ and trim(ls_mac) <> ” then

              //因为使用DHCP动态分配IP,所以根据MAC地址来标识唯一的计算机

              ll_row=dw_cx.find(“mac=’”+ls_mac+”‘”,1,dw_cx.rowcount())

              if ll_row>0 then

                     //如果原来有数据则修改

                     dw_cx.o b j e c t.mac[ll_row]=ls_mac

                     dw_cx.o b j e c t.pcname[ll_row]=ls_pcname

                     dw_cx.o b j e c t.workgroup[ll_row]=ls_group

                     dw_cx.o b j e c t.ip[ll_row]=as_ip

                     dw_cx.o b j e c t.online[ll_row]=1

              else

                     ll_row=dw_cx.insertrow(0)

                     dw_cx.o b j e c t.rowid[ll_row]=0

                     dw_cx.o b j e c t.mac[ll_row]=ls_mac

                     dw_cx.o b j e c t.pcname[ll_row]=ls_pcname

                     dw_cx.o b j e c t.workgroup[ll_row]=ls_group

                     dw_cx.o b j e c t.ip[ll_row]=as_ip

                     dw_cx.o b j e c t.online[ll_row]=1

              end if

       end if

end if

 

2 ===以下是我从上面精减成一个只取mac地址的函数(fx_getmac_dos)

定义api

Function boolean IsWindow (Long hwnd ) Library "user32.dll"
FUNCTION ulong WinExec(ref string lpCmdLine,ulong nCmdShow) LIBRARY "kernel32.dll"

string        ls_temp,as_ip

long              ll_row,p

integer        li_filenum

ulong        ll_handle

string        ls_pcname,ls_mac,ls_group

as_ip="jack1"  //此处是计算机名或ip都可

ls_temp="nbtstat -a "+as_ip+" >c:\"+as_ip+".txt"


li_FileNum = FileOpen("run.bat",StreamMode!, Write!, LockWrite!, Replace!)

FileWrite(li_FileNum,ls_temp)

FileClose(li_filenum)

ls_temp='run.bat'

ll_handle=WinExec(ls_temp,0)

//等待DOS窗口关闭

Do While isWindow(ll_handle)

       Yield()
Loop


li_FileNum=FileOpen("c:\"+as_ip+".txt",StreamMode!,Read! )

if li_FileNum>0 then
       FileRead(li_FileNum,ls_temp)
     
       p=pos(ls_temp,'MAC Address = ')

       ls_mac=mid(ls_temp,p + 14,17)
     
//       if  ls_mac='\NetBT_Tcpip_{942′ then  ls_mac='其他设备'

       if ls_mac<>'其他设备' and trim(ls_mac) <> '' then
   
   messagebox("",ls_mac)
  
  end if
  
   FileClose(li_filenum)
       FileDelete("c:\"+as_ip+".txt")
   FileDelete("run.bat")

end if

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在用pb开发信息管理系统时,为了安全期间,我们希望记录每个登录用户的信息,并生成日志,其中最重要的是知道是哪个机器运行程序的?这样,只要获运行程序的机器ip问题就解决了,然而,pb中没有象delphi 中有现成的函数可以很方便的获的机器的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" 然后在form的open事件中执行如下代码就可以了 s_wsadata l_WSAData 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) //获得主机IP地址 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") //将IP地址的ASC码形式转化为***.***.***.***字符串格式 END IF WSACleanup() ELSE messagebox("GetHostName",WSAGetLastError()) END IF
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值