PRB: WNetEnumResource Returns Different Info on Win95 and NT

ID: Q152823

The information in this article applies to:

- Microsoft Win32 Software Development KIT (SDK) for Windows NT,

  version 3.5
- Microsoft Win32 Software Development KIT (SDK) for Windows 95

SYMPTOMS

When the WINNET example from the January 96 MSDN is built on Windows 95, and the sample is executed at the MS-DOS prompt from within Windows 95, the following error is displayed:

   This program has performed an illegal operation and will be shutdown.
   If the problem persists, contact the program vendor.

If the WINNET example is executed within the Visual C++ 4.0 debug environment, the following error is received:

   Unhandled exception in winnet.exe: 0xC0000005 : Access Violation.

CAUSE

The WNetEnumResouce API returns different values on Win95 than on Windows NT.

The problem occurs when an enumeration starts at the root of the network. The first buffer of NETRESOURCE structures returned from WNetEnumResource is different. On Windows 95, the lpProvider field is set to the Network Provider name and the lpRemoteName is set to NULL. On NT, the lpProvider and lpRemoteName fields point to different strings containing the same values.

In the WINNET sample, when the EnumResource function tries to execute its example filter, the _strnicmp C Run-Time function causes an ACCESS VIOLATION because the lpRemoteName is NULL.

RESOLUTION

Checking the value of the lpRemoteName field and ensuring it is pointing to a valid address before allowing the filter to execute prevents the ACCESS VIOLATION error.

STATUS

This behavior is by design.

MORE INFORMATION

The difference between the way the WNetEnumResource API works on NT and Windows 95 has only been observed when starting an enumeration at the root of the network. On Windows 95 platforms, it becomes necessary to check the value returned in the lpRemoteName for the first set of structures returned from WNetEnumResource.

Sample Code

   /* Compile options needed:
     Be sure to include the mpr.lib library when linking.
   */ 
   #include <windows.h>
   #include <winnetwk.h>
   #include <stdio.h>

   void main ( void )
   {
      NETRESOURCE resourcebuffer[5000];
      HANDLE hEnum;
      DWORD  netRet, dwEntriesToGet=0XFFFFFFFF;
      DWORD  dwSizeResourcebuffer = sizeof( resourcebuffer);

      // 
      // Enumerate starting at the root of the network
      // 
      netRet = WNetOpenEnum( RESOURCE_GLOBALNET,
                             RESOURCETYPE_DISK,
                             (RESOURCEUSAGE_CONNECTABLE |
    RESOUCEUSAGE_CONTAINER),
                             (LPNETRESOURCE)NULL,
                             &hEnum);
     if( netRet == NO_ERROR )
     {
        // 
        // The WNetOpenEnum was successful. Now get all the network
   providers
    for
        // the network.
        // 
        netRet = WNetEnumResource( hEnum,
                                   (LPDWORD)&dwEntriesToGet,
                                   (LPVOID)resourcebuffer,
                                   (LPDWORD)&dwSizeResourcebuffer);
        if( netRet == NO_ERROR )
        {
           // 
           // The resourcebuffer contains the network providers. Take a
   look at
           // the lpRemoteName field. ON win 95, this field will be NULL,
   on NT
           // the field will match the lpProvider field
           // 
           DWORD i;
           for( i = 0; i < dwEntriesToGet; i++ )
           {
              if( resourcebuffer[i].lpRemoteName ) printf("Remote Name:
   %s\n",
                     resourcebuffer[i].lpRemoteName);
              else printf("Remote Name: IS NULL\n");
              printf( "Network Provider: %s\n\n",
   resourcebuffer[i].lpProvider );
           }
        }
        else printf("ERROR: WNetEnumResource API failed : %ld\n", netRet );
    }
    else printf("ERROR: WNetOpenEnum API Failed: %ld\n", netRet);

   }

Additional query words:
Keywords          : kbnetwork kbnokeyword kbAPI kbNTOS350 kbSDKPlatform kbWinOS95 kbGrpNet 
Issue type        : kbprb

Last Reviewed: July 31, 1998