while(dwResultEnum != ERROR_NO_MORE_ITEMS)
  {  
    //
    // Initialize the buffer.
    //
    ZeroMemory(lpnrLocal, cbBuffer);
    //
    // Call the WNetEnumResource function to continue
    //  the enumeration.
    //
    dwResultEnum = WNetEnumResource(hEnum,      // resource handle
                                    &cEntries,  // defined locally as -1
                                    lpnrLocal,  // LPNETRESOURCE
                                    &cbBuffer); // buffer size
   
  }msdn上说循环调用它就可以得到所有的网络资源!
我但不跟踪发现第一次执行WNetEnumResource时可以得到第一个网络资源,并且cEntries = 7,说明有7个资源共享了。
可是循环到第2次时返回值dwResultEnum 时259(ERROR_NO_MORE_ITEMS)
为什么?

解决方案 »

  1.   

    你得到的资源中间有的还包括子集,这类资源像容器一样
    BOOL WINAPI EnumerateFunc(HWND hwnd, 
                              HDC hdc, 
                              LPNETRESOURCE lpnr)

      DWORD dwResult, dwResultEnum;
      HANDLE hEnum;
      DWORD cbBuffer = 16384;      // 16K is a good size
      DWORD cEntries = -1;         // enumerate all possible entries
      LPNETRESOURCE lpnrLocal;     // pointer to enumerated structures
      DWORD i;
      //
      // Call the WNetOpenEnum function to begin the enumeration.
      //
      dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
                              RESOURCETYPE_ANY,   // all resources
                              0,        // enumerate all resources
                              lpnr,     // NULL first time the function is called
                              &hEnum);  // handle to the resource  if (dwResult != NO_ERROR)
      {  
        //
        // Process errors with an application-defined error handler.
        //
        NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum");
        return FALSE;
      }
      //
      // Call the GlobalAlloc function to allocate resources.
      //
      lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
      
      do
      {  
        //
        // Initialize the buffer.
        //
        ZeroMemory(lpnrLocal, cbBuffer);
        //
        // Call the WNetEnumResource function to continue
        //  the enumeration.
        //
        dwResultEnum = WNetEnumResource(hEnum,      // resource handle
                                        &cEntries,  // defined locally as -1
                                        lpnrLocal,  // LPNETRESOURCE
                                        &cbBuffer); // buffer size
        //
        // If the call succeeds, loop through the structures.
        //
        if (dwResultEnum == NO_ERROR)
        {
          for(i = 0; i < cEntries; i++)
          {
            // Call an application-defined function to
            //  display the contents of the NETRESOURCE structures.
            //
            DisplayStruct(hdc, &lpnrLocal[i]);        // If the NETRESOURCE structure represents a container resource, 
            //  call the EnumerateFunc function recursively.        if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
                                           & RESOURCEUSAGE_CONTAINER))
              if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i]))
                TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29);
          }
        }
        // Process errors.
        //
        else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
        {
          NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");
          break;
        }
      }
      //
      // End do.
      //
      while(dwResultEnum != ERROR_NO_MORE_ITEMS);
      //
      // Call the GlobalFree function to free the memory.
      //
      GlobalFree((HGLOBAL)lpnrLocal);
      //
      // Call WNetCloseEnum to end the enumeration.
      //
      dwResult = WNetCloseEnum(hEnum);
      
      if(dwResult != NO_ERROR)
      { 
        //
        // Process errors.
        //
        NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
        return FALSE;
      }  return TRUE;
    }你看见中间的RESOURCEUSAGE_CONTAINER 没有,在她里面还有东西
      

  2.   


    能不能帮我解释一下这句我没看懂
    if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage& RESOURCEUSAGE_CONTAINER))
    特别是lpnrLocal[i].dwUsage& RESOURCEUSAGE_CONTAINER    这是什么意识?我不懂这个语法
    lpnrLocal是个结构体数组吗?那& RESOURCEUSAGE_CONTAINER    又是什么东西?
    谢谢
      

  3.   

    lpnrLocal[i].dwUsage& RESOURCEUSAGE_CONTAINER    是按为与,表示这个是一个容器类的资源
    你打开网上邻居,可以看到“邻近的计算机”,这个就是一个容器类的资源,要对它再递归才可以得到主机,然后对主机再递归,有可以得到其它的资源
    我用这个函数做了一个程序,可以列举局域网上的机器
      

  4.   

    能不能给我一份你的程序和相关的资料。谢谢大虾了
    [email protected]
    十分感谢
      

  5.   

    你打开网上邻居,可以看到“邻近的计算机”,这个就是一个容器类的资源,要对它再递归才可以得到主机,然后对主机再递归,有可以得到其它的资源递归?为什么要递归啊!!
    我没明白lpnrLocal[i]我直接用它就得到了7个资源
    lpnrLocal[0]-lpnrLocal[6]请大虾给讲讲!!
    谢谢