WNetEnumResource returns incomplete results

2.1k views Asked by At

i writing a program that lists Windows network shared objects, using WNet functions. i noticed that, on some reason, WNetEnumResource function retrieve just my local shares, although the explorer sees much more objects. what could be causing this? here's code:

    dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
        RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER, lpnr, &hEnum);
................
        dwResultEnum = WNetEnumResource(hEnum, &cEntries, lpnrLocal, &cbBuffer);

that's what i got:

Microsoft Terminal Services network
Microsoft Windows Network   network
    WORKGROUP   domain
        \\MOSKKM    server
            \\MOSKKM\HP LaserJet 3055 PCL5  share
            \\MOSKKM\share  share
            \\MOSKKM\soft   share
            \\MOSKKM\tunnel share
Web Client Network  network

my system is Win7, i use an administrator account, and no antiviruses or firewalls running. and also on bridged VirtualBox VM hosted on the same machine it works correct (however, for a long time). what's might be wrong? maybe there are some other ways to solve this problem, not using WNet functions?

and also: WNetEnumResource gives up shared objects sorted by alphabet. i can't imagine any approach how to parallelize this process. any ideas?

1

There are 1 answers

0
halcyonx On

I think you need to write recursive function to enumerate the network resources,

To enumerate all network resources, an application can begin the enumeration by calling WNetOpenEnum with the lpNetResource parameter set to NULL, and then use the returned handle to call WNetEnumResource to enumerate resources. If one of the resources in the NETRESOURCE array returned by the WNetEnumResource function is a container resource, you can call WNetOpenEnum to open the resource for further enumeration. (MSDN: WNetOpenEnum)

int EnumNetRes(NETRESOURCE *lpNetRes)

{

  DWORD dwResult;
  DWORD dwCount = -1;
  DWORD dwSize = sizeof(NETRESOURCE)*MAX_NET_RESOURCES;
  HANDLE hEnum;  

  dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
    RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER, lpNetRes, &hEnum);

  if(dwResult != NO_ERROR)
  {
       cerr << "WNetOpenEnum failed, error: " << dwResult << endl;
       //...
       return 1;
  }

 NETRESOURCE NetResources[MAX_NET_RESOURCES];  // #define MAX_NET_RESOURCES 1024
 dwResult = WNetEnumResource(hEnum, &dwCount, NetResource, dwSize);

 if(dwResult != NO_ERROR)
 {
     //...
     return 1;
 }


 for( int i=0; i<dwCount; i++){
      //...
      // retrieve of information about resource 
      //...

 if(NetResources[i].dwUsage & RESOURCEUSAGE_CONTAINER)
     EnumNetRes(&NetResources[i]);
 }

}