'在模块中
Option ExplicitPrivate 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 TypePrivate 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 LongPrivate Declare Function WNetEnumResource Lib "mpr.dll" Alias _
  "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
  ByVal lpBuffer As Long, lpBufferSize As Long) As LongPrivate Declare Function WNetCloseEnum Lib "mpr.dll" _
   (ByVal hEnum As Long) As LongPrivate Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCE_REMEMBERED = &H3Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFFPrivate Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const RESOURCEUSAGE_RESERVED = &H80000000Private 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 LongPrivate Declare Function GlobalFree Lib "KERNEL32" _
  (ByVal hMem As Long) As LongPrivate 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 LongPublic Function DoNetEnum(list As Object) As Boolean'PURPOSE: DISPLAYS NETWORK NAME AND
'ALL COMPUTERS ON THE NETWORK IN A LIST
'BOX'PARAMETER: ListBox (or any control with similar
'interface, such as ComboBox) in which to display
'list of computers'RETURNS: True if successful, false otherwiseDim 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 LongOn Error Resume Next
'test to see if list is a
'list box type control
list.AddItem " "
list.Clear
If Err.Number > 0 Then Exit FunctionOn Error GoTo ErrorHandler'Setup the NETRESOURCE input structure.
NR.lpRemoteName = 0
cbBuff = 10000
cCount = &HFFFFFFFF'Open a Net enumeration operation handle: hEnum.
res = WNetOpenEnum(RESOURCE_GLOBALNET, _
  RESOURCETYPE_ANY, 0, NR, hEnum)If res = 0 Then   'Create a buffer large enough for the results.
   '10000 bytes should be sufficient.
   lpBuff = GlobalAlloc(GPTR, cbBuff)
   'Call the enumeration function.
   res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
   If res = 0 Then
      p = lpBuff
      '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
         ' All we get back are the Global Network Containers --- Enumerate each of these
         CopyMemory NR, ByVal p, LenB(NR)
         list.AddItem "Network Name " & _
            PointerToString(NR.lpRemoteName)         DoNetEnum2 NR, list
         p = p + LenB(NR)
      Next i
      End If
DoNetEnum = TrueErrorHandler:
On Error Resume Next
   If lpBuff <> 0 Then GlobalFree (lpBuff)
   WNetCloseEnum (hEnum) 'Close the enumerationEnd IfEnd FunctionPrivate 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 FunctionPrivate Sub DoNetEnum2(NR As NETRESOURCE, list As Object)   Dim hEnum As Long, lpBuff As Long
   Dim cbBuff As Long, cCount As Long
   Dim p As Long, res As Long, i As Long   'Setup the NETRESOURCE input structure.
   cbBuff = 10000
   cCount = &HFFFFFFFF   'Open a Net enumeration operation handle: hEnum.
   res = WNetOpenEnum(RESOURCE_GLOBALNET, _
     RESOURCETYPE_ANY, 0, NR, hEnum)   If res = 0 Then      'Create a buffer large enough for the results.
      '10000 bytes should be sufficient.
      lpBuff = GlobalAlloc(GPTR, cbBuff)
      'Call the enumeration function.
      res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)      If res = 0 Then
         p = lpBuff
         'WNetEnumResource fills the buffer with an array of
         'NETRESOURCE structures. Walk through the list and print
         'each remote name.
         For i = 1 To cCount
            CopyMemory NR, ByVal p, LenB(NR)
            list.AddItem "Network Computer #" & i & " " & PointerToString(NR.lpRemoteName)
            p = p + LenB(NR)
         Next i
      End If      If lpBuff <> 0 Then GlobalFree (lpBuff)
      WNetCloseEnum (hEnum) 'Close the enumeration   End IfEnd Sub'在窗体中放入一个listbox控件
Private Sub Form_Load()
DoNetEnum List1
End Sub=========

解决方案 »

  1.   

    因为是局域网,所以IP地址的前三段应该是相同的。那么,你首先获得自己的IP地址(如192.168.0.20),然后你就从192.168.0.1一直PING到192.168.0.255。能够PING的通的,说明该IP是激活,然后通过程序获取对应的机器名。我试了一下,PING完一个C段的IP,大概需要10分钟左右,主要是根据局域网中机器的数量,和网络的复杂程度---香水百合
    MicroSoft提供了以下几个API:WnetOpenEnum、WnetEnumResource、WNetCloseEnum及一个结构类型NETRESOURCE用于搜索局域网上的网络资源。   其中NETRESOURCE的定义如下: typedef struct _NETRESOURCE { // nr 
     DWORD dwScope; 
     DWORD dwType; 
     DWORD dwDisplayType; 
     DWORD dwUsage; 
     LPTSTR lpLocalName; 
     LPTSTR lpRemoteName; 
     LPTSTR lpComment; 
     LPTSTR lpProvider; 
    } NETRESOURCE;    我们主要用到是dwType(资源类型:磁盘或打印机)、dwDisplayType(资源类型:服务器,域等)、dwUsage(判断是不是容器c o ntainer)、lpRemoteName(资源名称)。   WnetOpenEnum的作用是打开某一特定的资源容器句柄给WnetEnumResource。WnetEnumResource枚举出指定容器下所有网络资源。   下面是一个列出所有网络资源的的函数示例: bool __fastcall TForm1::EnumerateFunc(HWND hwnd ,LPNETRESOURCE lpnr,TTreeNode * ParentNode) 

    DWORD dwResult, dwResultEnum; 
    HANDLE hEnum; 
    DWORD cbBuffer = 16384; /* 16K is reasonable size */ 
    DWORD cEntries = 0xFFFFFFFF; /* enumerate all possible entries */ 
    LPNETRESOURCE lpnrLocal; /* pointer to enumerated structures */ 
    DWORD i; 
    dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, 
    RESOURCETYPE_ANY, 
    0, /* enumerate all resources */ 
    lpnr, /* NULL first time this function is called */ 
    &hEnum); /* handle to resource */ 
    if (dwResult ==67) //domain下没有可访问的主机 
    return true; 
    if (dwResult != NO_ERROR ) { //无 
    ShowError(dwResult); 
    return false; 

    TTreeNode * Child; 
    do { 
    lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer); dwResultEnum = WNetEnumResource(hEnum, /* resource handle */ 
    &cEntries, /* defined locally as 0xFFFFFFFF */ 
    lpnrLocal, /* LPNETRESOURCE */ 
    &cbBuffer); /* buffer size */ if (dwResultEnum == NO_ERROR) { 
    for(i = 0; i < cEntries; i++) { 
    Child=TreeView1->Items->AddChild(ParentNode,lpnrLocal[i].lpRemoteName); 
    if(RESOURCEUSAGE_c o nTAINER == 
    (lpnrLocal[i].dwUsage & RESOURCEUSAGE_c o nTAINER)) 
    if(!EnumerateFunc(this->Handle, &lpnrLocal[i],Child)) 
    ShowMessage("EnumerateFunc returned FALSE."); 

    } else if (dwResultEnum != ERROR_NO_MORE_ITEMS) { 
    ShowError(dwResultEnum); 
    break; 


    while(dwResultEnum != ERROR_NO_MORE_ITEMS); 
    GlobalFree((HGLOBAL) lpnrLocal); 
    dwResult = WNetCloseEnum(hEnum); 
    if(dwResult != NO_ERROR) { 
    ShowError(dwResult); 
    return FALSE; 

    return true; 
    }    需要说明在win98下,此方法列出的资源和网络邻居列出的是一样,和网络邻居一样,有时列出的数据并不准确,可能会有一些域或机器不能找到。 
      

  2.   

    挨个ping  不就可以了吗?  不就是255个呀 很快的
      

  3.   

    同意sunxl(じ小呆☆`~o) 测试同一网段的IP是否能连通