Platform SDK: Network Management

NetShareEnum

The NetShareEnum function retrieves information about each shared resource on a server.

Win32-based applications can also use the WNetEnumResource function. However, WNetEnumResource does not enumerate hidden shares or users connected to a share.

Security Requirements

Windows NT: Administrator or Communication, Print, or Server operator group membership is required to successfully execute the NetShareEnum function at level 2. No special group membership is required for level 0 or level 1 calls.

Windows 2000: If you call this function at information level 2 on a Windows 2000 domain controller that is running Active Directory, access is allowed or denied based on the access-control list (ACL) for the securable object. The default ACL permits all authenticated users and members of the "Pre-Windows 2000 compatible access" group to view the information. By default, the "Pre-Windows 2000 compatible access" group includes Everyone as a member. This enables anonymous access to the information if the system allows anonymous access.

If you call this function at information level 2 on a Windows 2000 member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the RestrictAnonymous policy setting allows anonymous access.

For more information about restricting anonymous access, see Security Requirements for the Network Management Functions.

Parameter Order

Windows NT/2000: The parameter order is as follows.

NET_API_STATUS NetShareEnum(
  LPWSTR servername,     
  DWORD level,           
  LPBYTE *bufptr,        
  DWORD prefmaxlen,      
  LPDWORD entriesread,   
  LPDWORD totalentries,  
  LPDWORD resume_handle  
);

Windows 95/98: The calling application must use the cbBuffer parameter to specify the size, in bytes, of the information buffer pointed to by the pbBuffer parameter. (The cbBuffer parameter replaces the Windows NT/Windows 2000 prefmaxlen parameter.) A resume handle parameter is not available on this platform. Therefore, the parameter list is as follows.

extern API_FUNCTION
 NetShareEnum(
  const char FAR * pszServer,         
  short sLevel,                       
  char FAR * pbBuffer,                
  unsigned short  cbBuffer,           
  unsigned short FAR * pcEntriesRead, 
  unsigned short FAR * pcTotalAvail   
);

Parameters

servername
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the name of the remote server on which the function is to execute. The string must begin with \\. If this parameter is NULL, the local computer is used.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.
Value Meaning
0 Windows NT/2000: Return share names. The bufptr parameter points to an array of SHARE_INFO_0 structures.
1 Return information about shared resources, including the name and type of the resource, and a comment associated with the resource.

Windows NT/2000: The bufptr parameter points to an array of SHARE_INFO_1 structures.

Windows 95/98: The pbBuffer parameter points to an array of share_info_1 structures.

2 Windows NT/2000: Return information about shared resources, including name of the resource, type and permissions, password, and number of connections. The bufptr parameter points to an array of SHARE_INFO_2 structures.
50 Windows 95/98: Return information about shared resources, including the name and type of the resource, a comment associated with the resource, and passwords. The pbBuffer parameter points to an array of share_info_50 structures.
502 Windows NT/2000: Return information about shared resources, including name of the resource, type and permissions, number of connections, and other pertinent information. The bufptr parameter points to an array of SHARE_INFO_502 structures.

bufptr
[out] Pointer to the buffer that receives the data. The format of this data depends on the value of the level parameter.

Windows NT/2000: This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA.

Windows 95/98: The caller must allocate and deallocate this buffer.

prefmaxlen
[in] Specifies the preferred maximum length of returned data, in bytes. If you specify MAX_PREFERRED_LENGTH, the function allocates the amount of memory required for the data. If you specify another value in this parameter, it can restrict the number of bytes that the function returns. If the buffer size is insufficient to hold all entries, the function returns ERROR_MORE_DATA. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths.
entriesread
[out] Pointer to a value that receives the count of elements actually enumerated.
totalentries
[out] Pointer to a value that receives the total number of entries that could have been enumerated.
resume_handle
[in/out] Pointer to a DWORD value that contains a resume handle which is used to continue an existing share search. The handle should be zero on the first call and left unchanged for subsequent calls. If resume_handle is NULL, then no resume handle is stored.

Return Values

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value is a Win32 API error code. For a list of error codes, see Error Codes.

Remarks

To retrieve a value that indicates whether a share is the root volume in a Dfs tree structure, you must call the NetShareGetInfo function and specify information level 1005.

Windows 95/98: See the NetShareEnum Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetShareEnum function.

Windows NT/2000: The following code sample demonstrates how to retrieve information about each shared resource on a server using a call to the NetShareEnum function. The sample calls NetShareEnum, specifying information level 502 (SHARE_INFO_502). If the call succeeds, the code loops through the entries and prints information about each share. The sample also calls the IsValidSecurityDescriptor function to validate the shi502_security_descriptor member. Finally, the code sample frees the memory allocated for the information buffer.

#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>

void wmain( int argc, TCHAR *lpszArgv[ ])
{
   PSHARE_INFO_502 BufPtr,p;
   NET_API_STATUS res;
   LPTSTR   lpszServer = NULL;
   DWORD er=0,tr=0,resume=0, i;

   switch(argc)
   {
   case 2:
      lpszServer = lpszArgv[2];
      break;
   default:
      printf("Usage: NetShareEnum <servername>\n");
      return;
   }
   //
   // Print a report header.
   //
   printf("Share:              Local Path:                   Uses:   Descriptor:\n");
   printf("---------------------------------------------------------------------\n");
   //
   // Call the NetShareEnum function; specify level 502.
   //
   do // begin do
   {
      res = NetShareEnum (lpszServer, 502, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
      //
      // If the call succeeds,
      //
      if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
      {
         p=BufPtr;
         //
         // Loop through the entries;
         //  print retrieved data.
         //
         for(i=1;i<=er;i++)
         {
            printf("%-20S%-30S%-8u",p->shi502_netname, p->shi502_path, p->shi502_current_uses);
            //
            // Validate the value of the 
            //  shi502_security_descriptor member.
            //
            if (IsValidSecurityDescriptor(p->shi502_security_descriptor))
               printf("Yes\n");
            else
               printf("No\n");
            p++;
         }
         //
         // Free the allocated buffer.
         //
         NetApiBufferFree(BufPtr);
      }
      else 
         printf("Error: %ld\n",res);
   }
   // Continue to call NetShareEnum while 
   //  there are more entries. 
   // 
   while (res==ERROR_MORE_DATA); // end do
   return;
}

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions. For more information, see IADsFileShare.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
  Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).

See Also

Network Management Overview, Network Management Functions, Share Functions, SHARE_INFO_0, SHARE_INFO_1, SHARE_INFO_2, SHARE_INFO_502, share_info_1, share_info_50