我用WebBrowser 访问内网的其他机器,比如说:192.168.1.6
使用的方法是:WebBrowser1.Navigate2 "\\192.168.1.6",
但是如果内网中 192.168.1.6 这个地址不存在,那么,就会出现好几十秒种的死机现象?请问有没有办法先判断这个地址到底“可达”不啊?如果可达,就用WebBrowser1.Navigate2 "\\192.168.1.6"语句,如果不可达,则提示没这个地址这个怎么实现啊?

解决方案 »

  1.   

    使用你的App之前得到局域网所有的机器以及IP到一个文件或者数据库,然后Navigate2之前先检测文件或者数据库是否存在该IP!得到局域网所有的机器以及IP代码为:
    'Form1
    Option Explicit      Private Const GMEM_FIXED = &H0
          Private Const GMEM_ZEROINIT = &H40
          Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)      Private Declare Function GlobalAlloc Lib "KERNEL32" ( _
             ByVal wFlags As Long, ByVal dwBytes As Long) As Long
          Private Declare Function GlobalFree Lib "KERNEL32" ( _
             ByVal hMem As Long) As Long      Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
            (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)      Private Declare Function CopyPointer2String Lib "KERNEL32" _
             Alias "lstrcpyA" ( _
             ByVal NewString As String, ByVal OldString As Long) As Long      Private Sub Form_click()
             Dim hEnum As Long, lpBuff As Long, nr As NETRESOURCE
             Dim cbBuff As Long, cCount As Long
             Dim p As Long, res As Long, i As Long         'Setup the NETRESOURCE input structure.
             nr.dwUsage = RESOURCEUSAGE_CONTAINER
             nr.lpRemoteName = 0
             cbBuff = 1000
             cCount = &HFFFFFFFF         'Open a Net enumeration operation handle: hEnum.
             res = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
                                0, nr, hEnum)
             If res = 0 Then
                'Create a buffer large enough for the results.
                '1000 bytes should be sufficient.
                lpBuff = GlobalAlloc(GPTR, cbBuff)
                'Call the enumeration function.
                res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
                If res = 0 Then
                   p = lpBuff
                   Cls
                   'WNetEnumResource fills the buffer with an array of
                   'NETRESOURCE structures. Walk through the list and print
                   'each local and remote name.
                   For i = 1 To cCount
                      CopyMemory nr, ByVal p, LenB(nr)
                      p = p + LenB(nr)
                      Print PointerToString(nr.lpLocalName), _
                            PointerToString(nr.lpRemoteName)
                   Next i
                Else
                   MsgBox "Error: " & Err.LastDllError, vbOKOnly, _
                          "WNetEnumResources"
                End If
                If lpBuff <> 0 Then GlobalFree (lpBuff)
                WNetCloseEnum (hEnum) 'Close the enumeration
             Else
                MsgBox "Error: " & Err.LastDllError, vbOKOnly, "WNetOpenEnum"
             End If
          End Sub      Private Function PointerToString(p As Long) As String
             'The values returned in the NETRESOURCE structures are pointers to
             'ANSI strings so they need to be converted to Visual Basic
       Strings.
             Dim s As String
             s = String(255, Chr$(0))
             CopyPointer2String s, p
             PointerToString = Left(s, InStr(s, Chr$(0)) - 1)
          End Function'Module1
    Option Explicit      Public Type NETRESOURCE
                  dwScope As Long
                  dwType As Long
                  dwDisplayType As Long
                  dwUsage As Long
                  lpLocalName As Long
                  lpRemoteName As Long
                  lpComment As Long
                  lpProvider As Long
          End Type      Public 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      Public Declare Function WNetEnumResource Lib "mpr.dll" Alias _
              "WNetEnumResourceA" ( _
              ByVal hEnum As Long, _
              lpcCount As Long, _
              ByVal lpBuffer As Long, _
              lpBufferSize As Long) As Long      Public Declare Function WNetCloseEnum Lib "mpr.dll" ( _
              ByVal hEnum As Long) As Long      'RESOURCE ENUMERATION.
          Public Const RESOURCE_CONNECTED = &H1
          Public Const RESOURCE_GLOBALNET = &H2
          Public Const RESOURCE_REMEMBERED = &H3      Public Const RESOURCETYPE_ANY = &H0
          Public Const RESOURCETYPE_DISK = &H1
          Public Const RESOURCETYPE_PRINT = &H2
          Public Const RESOURCETYPE_UNKNOWN = &HFFFF      Public Const RESOURCEUSAGE_CONNECTABLE = &H1
          Public Const RESOURCEUSAGE_CONTAINER = &H2
          Public Const RESOURCEUSAGE_RESERVED = &H80000000
      

  2.   

    to:lxcc(虫子|专注于抢分)
    每台机器都设置了共享用户名密码的,他们要求在软件里提示用户名、密码,使用软件要用统一密码进去。也就是说,内网IP是事先存放在数据库里的,不能动态搜索。你这个是个好方法,请指教一下怎么调用,让我多学一找,呵呵,谢谢。
      

  3.   

    请指教一下怎么调用
    //上面的代码在Form_Click事件内把所有的局域网内的所有IP Print到Form上你把他保存到文件或者数据库即可!
      

  4.   

    lxcc(虫子|专注于抢分:我按照你说的做了,就出来错误提示:错误编号为:259form 上出不来IP
      

  5.   

    你的OS是什么!我以前在2K试过!现在系统是XP,也出错误! :$原文在这里
    http://support.microsoft.com/?kbid=177697
      

  6.   

    sorry!刚才看了!确实不支持XP
    只支持
    &#8226; Microsoft Windows NT 3.51 Service Pack 5 
    &#8226; Microsoft Windows NT 4.0 Service Pack 7 
    &#8226; Microsoft Windows 95 Service Pack 1 
    &#8226; Microsoft Win32 Software Development Kit (SDK) 
    &#8226; Microsoft Windows 2000 Standard Edition 
    &#8226; Microsoft Windows Millennium Edition 
      

  7.   

    如果也是XP
    试试
    http://vbnet.mvps.org/index.html?code/network/netserverenum.htm
    '添加List和CommandButton各一控件
    Option Explicit
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Copyright ?1996-2004 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Distribution: You can freely use this code in your own
    '               applications, but you may not reproduce
    '               or publish this code on any web site,
    '               online service, or distribute as source
    '               on any media without express permission.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Windows type used to call the Net API
    Private Const MAX_PREFERRED_LENGTH As Long = -1
    Private Const NERR_SUCCESS As Long = 0&
    Private Const ERROR_MORE_DATA As Long = 234&Private Const SV_TYPE_WORKSTATION         As Long = &H1
    Private Const SV_TYPE_SERVER              As Long = &H2
    Private Const SV_TYPE_SQLSERVER           As Long = &H4
    Private Const SV_TYPE_DOMAIN_CTRL         As Long = &H8
    Private Const SV_TYPE_DOMAIN_BAKCTRL      As Long = &H10
    Private Const SV_TYPE_TIME_SOURCE         As Long = &H20
    Private Const SV_TYPE_AFP                 As Long = &H40
    Private Const SV_TYPE_NOVELL              As Long = &H80
    Private Const SV_TYPE_DOMAIN_MEMBER       As Long = &H100
    Private Const SV_TYPE_PRINTQ_SERVER       As Long = &H200
    Private Const SV_TYPE_DIALIN_SERVER       As Long = &H400
    Private Const SV_TYPE_XENIX_SERVER        As Long = &H800
    Private Const SV_TYPE_SERVER_UNIX         As Long = SV_TYPE_XENIX_SERVER
    Private Const SV_TYPE_NT                  As Long = &H1000
    Private Const SV_TYPE_WFW                 As Long = &H2000
    Private Const SV_TYPE_SERVER_MFPN         As Long = &H4000
    Private Const SV_TYPE_SERVER_NT           As Long = &H8000
    Private Const SV_TYPE_POTENTIAL_BROWSER   As Long = &H10000
    Private Const SV_TYPE_BACKUP_BROWSER      As Long = &H20000
    Private Const SV_TYPE_MASTER_BROWSER      As Long = &H40000
    Private Const SV_TYPE_DOMAIN_MASTER       As Long = &H80000
    Private Const SV_TYPE_SERVER_OSF          As Long = &H100000
    Private Const SV_TYPE_SERVER_VMS          As Long = &H200000
    Private Const SV_TYPE_WINDOWS             As Long = &H400000  'Windows95 and above
    Private Const SV_TYPE_DFS                 As Long = &H800000  'Root of a DFS tree
    Private Const SV_TYPE_CLUSTER_NT          As Long = &H1000000 'NT Cluster
    Private Const SV_TYPE_TERMINALSERVER      As Long = &H2000000 'Terminal Server
    Private Const SV_TYPE_DCE                 As Long = &H10000000 'IBM DSS
    Private Const SV_TYPE_ALTERNATE_XPORT     As Long = &H20000000 'rtn alternate transport
    Private Const SV_TYPE_LOCAL_LIST_ONLY     As Long = &H40000000 'rtn local only
    Private Const SV_TYPE_DOMAIN_ENUM         As Long = &H80000000
    Private Const SV_TYPE_ALL                 As Long = &HFFFFFFFFPrivate Const SV_PLATFORM_ID_OS2       As Long = 400
    Private Const SV_PLATFORM_ID_NT        As Long = 500'Mask applied to svX_version_major in
    'order to obtain the major version number.
    Private Const MAJOR_VERSION_MASK        As Long = &HFPrivate Type SERVER_INFO_100
      sv100_platform_id As Long
      sv100_name As Long
    End TypePrivate Declare Function NetServerEnum Lib "netapi32" _
      (ByVal servername As Long, _
       ByVal level As Long, _
       buf As Any, _
       ByVal prefmaxlen As Long, _
       entriesread As Long, _
       totalentries As Long, _
       ByVal servertype As Long, _
       ByVal domain As Long, _
       resume_handle As Long) As LongPrivate Declare Function NetApiBufferFree Lib "netapi32" _
       (ByVal Buffer As Long) As LongPrivate Declare Sub CopyMemory Lib "kernel32" _
       Alias "RtlMoveMemory" _
      (pTo As Any, uFrom As Any, _
       ByVal lSize As Long)
       
    Private Declare Function lstrlenW Lib "kernel32" _
      (ByVal lpString As Long) As Long
    Private Sub Form_Load()   Command1.Caption = "Net Server Enum"End Sub
       Private Sub Command1_Click()   Call GetServers(vbNullString)
     
    End Sub
    Private Function GetServers(sDomain As String) As Long  'lists all servers of the specified type
      'that are visible in a domain.
      
       Dim bufptr          As Long
       Dim dwEntriesread   As Long
       Dim dwTotalentries  As Long
       Dim dwResumehandle  As Long
       Dim se100           As SERVER_INFO_100
       Dim success         As Long
       Dim nStructSize     As Long
       Dim cnt             As Long   nStructSize = LenB(se100)
       
      'Call passing MAX_PREFERRED_LENGTH to have the
      'API allocate required memory for the return values.
      '
      'The call is enumerating all machines on the
      'network (SV_TYPE_ALL); however, by Or'ing
      'specific bit masks for defined types you can
      'customize the returned data. For example, a
      'value of 0x00000003 combines the bit masks for
      'SV_TYPE_WORKSTATION (0x00000001) and
      'SV_TYPE_SERVER (0x00000002).
      '
      'dwServerName must be Null. The level parameter
      '(100 here) specifies the data structure being
      'used (in this case a SERVER_INFO_100 structure).
      '
      'The domain member is passed as Null, indicating
      'machines on the primary domain are to be retrieved.
      'If you decide to use this member, pass
      'StrPtr("domain name"), not the string itself.
       success = NetServerEnum(0&, _
                               100, _
                               bufptr, _
                               MAX_PREFERRED_LENGTH, _
                               dwEntriesread, _
                               dwTotalentries, _
                               SV_TYPE_ALL, _
                               0&, _
                               dwResumehandle)  'if all goes well
       If success = NERR_SUCCESS And _
          success <> ERROR_MORE_DATA Then
          
        'loop through the returned data, adding each
        'machine to the list
          For cnt = 0 To dwEntriesread - 1
             
            'get one chunk of data and cast
            'into an SERVER_INFO_100 struct
            'in order to add the name to a list
             CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize
                
             List1.AddItem GetPointerToByteStringW(se100.sv100_name)
             
          Next
          
       End If
       
      'clean up regardless of success
       Call NetApiBufferFree(bufptr)
       
      'return entries as sign of success
       GetServers = dwEntriesreadEnd Function
    Public Function GetPointerToByteStringW(ByVal dwData As Long) As String
      
       Dim tmp() As Byte
       Dim tmplen As Long
       
       If dwData <> 0 Then
       
          tmplen = lstrlenW(dwData) * 2
          
          If tmplen <> 0 Then
          
             ReDim tmp(0 To (tmplen - 1)) As Byte
             CopyMemory tmp(0), ByVal dwData, tmplen
             GetPointerToByteStringW = tmp
             
         End If
         
       End If
        
    End Function
      

  8.   

    得到的是机器名称,而不是内网IP呀,怎么才能得到IP呢?
      

  9.   

    to:lxcc(虫子|专注于抢分),谢谢,问题解决,接分。