VB中获取网上邻居里的计算机名

发表于:2007-06-21来源:作者:点击数: 标签:
option explicit private const resource_connected as long = h1 private const resource_globa .net as long = h2 private const resource_remembered as long = h3 private const resourcedisplaytype_directory = h9 private const resourcedisplaytype_

   

option explicit
private const resource_connected as long = &h1&
private const resource_globa.net as long = &h2&

private const resource_remembered as long = &h3&

private const resourcedisplaytype_directory& = &h9
private const resourcedisplaytype_domain& = &h1
private const resourcedisplaytype_file& = &h4
private const resourcedisplaytype_generic& = &h0
private const resourcedisplaytype_group& = &h5
private const resourcedisplaytype_network& = &h6
private const resourcedisplaytype_root& = &h7
private const resourcedisplaytype_server& = &h2
private const resourcedisplaytype_share& = &h3
private const resourcedisplaytype_shareadmin& = &h8
private const resourcetype_any as long = &h0&
private const resourcetype_disk as long = &h1&
private const resourcetype_print as long = &h2&
private const resourcetype_unknown as long = &hffff&
private const resourceusage_all as long = &h0&
private const resourceusage_connectable as long = &h1&
private const resourceusage_container as long = &h2&
private const resourceusage_reserved as long = &h80000000
private const no_error = 0
private const error_more_data = 234 'l    // dderror
private const resource_enum_all as long = &hffff
private type netresource
   dwscope as long
   dwtype as long
   dwdisplaytype as long
   dwusage as long
   plocalname as long
   premotename as long
   pcomment as long
   pprovider as long
end type
private type netresource_real
   dwscope as long
   dwtype as long
   dwdisplaytype as long
   dwusage as long
   slocalname as string
   sremotename as string
   scomment as string
   sprovider as string
end type
private declare function wnetaddconnection2 lib "mpr.dll" alias "wnetaddconnection2a" (lpnetresource as netresource, byval lppassword as string, byval lpusername as string, byval dwflags as long) as long
private declare function wnetopenenum lib "mpr.dll" alias "wnetopenenuma" (byval dwscope as long, byval dwtype as long, byval dwusage as long, lpnetresource as any, lphenum as long) as long
private declare function wnetenumresource lib "mpr.dll" alias "wnetenumresourcea" (byval henum as long, lpclearcase/" target="_blank" >ccount as long, lpbuffer as netresource, lpbuffersize as long) as long
private declare function wnetcloseenum lib "mpr.dll" (byval henum as long) as long
private declare function varptrany lib "vb40032.dll" alias "varptr" (lpobject as any) as long
private declare sub copymem lib "kernel32" alias "rtlmovememory" (lpto as any, lpfrom as any, byval llen as long)
private declare sub copymembyptr lib "kernel32" alias "rtlmovememory" (byval lpto as long, byval lpfrom as long, byval llen as long)
private declare function lstrcpy lib "kernel32" alias "lstrcpya" (byval lpstring1 as string, byval lpstring2 as any) as long
private declare function lstrlen lib "kernel32" alias "lstrlena" (byval lpstring as any) as long
sub main()
   const max_resources = 256
   const not_a_container = -1

   dim bfirsttime as boolean
   dim lreturn as long
   dim henum as long
   dim lcount as long
   dim lmin as long
   dim llength as long
   dim l as long
   dim lbuffersize as long
   dim llastindex as long
   dim unetapi(0 to max_resources) as netresource
   dim unet() as netresource_real
   bfirsttime = true
   do
       if bfirsttime then
           lreturn = wnetopenenum(resource_globalnet, resourcetype_any, resourceusage_all, byval 0&, henum)
           bfirsttime = false
       else
           if unet(llastindex).dwusage and resourceusage_container then
               lreturn = wnetopenenum(resource_globalnet, resourcetype_any, resourceusage_all, unet(llastindex), henum)
           else
               lreturn = not_a_container
               henum = 0
           end if
           llastindex = llastindex + 1
       end if
       if lreturn = no_error then
           lcount = resource_enum_all
           do
               lbuffersize = ubound(unetapi) * len(unetapi(0)) / 2
               lreturn = wnetenumresource(henum, lcount, unetapi(0), lbuffersize)
               if lcount > 0 then
                   redim preserve unet(0 to lmin + lcount - 1) as netresource_real
                   for l = 0 to lcount - 1
                       'each resource will appear here as unet(i)
                       unet(lmin + l).dwscope = unetapi(l).dwscope
                       unet(lmin + l).dwtype = unetapi(l).dwtype
                       unet(lmin + l).dwdisplaytype = unetapi(l).dwdisplaytype
                       unet(lmin + l).dwusage = unetapi(l).dwusage
                       if unetapi(l).plocalname then
                           llength = lstrlen(unetapi(l).plocalname)
                           unet(lmin + l).slocalname = space$(llength)
                           copymem byval unet(lmin + l).slocalname, byval unetapi(l).plocalname, llength
                       end if
                       if unetapi(l).premotename then
                           llength = lstrlen(unetapi(l).premotename)
                           unet(lmin + l).sremotename = space$(llength)
                           copymem byval unet(lmin + l).sremotename, byval unetapi(l).premotename, llength
                       end if
if unetapi(l).pcomment then
                           llength = lstrlen(unetapi(l).pcomment)
                           unet(lmin + l).scomment = space$(llength)
                           copymem byval unet(lmin + l).scomment, byval unetapi(l).pcomment, llength
                       end if
                       if unetapi(l).pprovider then
                           llength = lstrlen(unetapi(l).pprovider)
                           unet(lmin + l).sprovider = space$(llength)
                           copymem byval unet(lmin + l).sprovider, byval unetapi(l).pprovider, llength
                       end if
                   next l
               end if
               lmin = lmin + lcount
           loop while lreturn = error_more_data
       end if
       if henum then
           l = wnetcloseenum(henum)
       end if
   loop while llastindex < lmin
   if ubound(unet) > 0 then
       for l = 0 to ubound(unet)
           select case unet(l).dwdisplaytype
               case resourcedisplaytype_directory&
                   debug.print "directory...",
               case resourcedisplaytype_domain
                   debug.print "domain...",
               case resourcedisplaytype_file
                   debug.print "file...",
               case resourcedisplaytype_generic
                   debug.print "generic...",
               case resourcedisplaytype_group
                   debug.print "group...",
               case resourcedisplaytype_network&
                   debug.print "network...",
               case resourcedisplaytype_root&
                   debug.print "root...",
               case resourcedisplaytype_server
                   debug.print "server...",
               case resourcedisplaytype_share
                   debug.print "share...",
               case resourcedisplaytype_shareadmin&
                   debug.print "shareadmin...",
           end select
           debug.print unet(l).sremotename, unet(l).scomment
       next l
   end if

原文转自:http://www.ltesting.net