The Win32 Network Management APIs 42

 

 

 

 

 

NetShareEnum() Program Example

 

This function retrieves information about each shared resource on a server. You can also use the WNetEnumResource() function to retrieve resource information. However, WNetEnumResource() does not enumerate hidden shares or users connected to a share. For interactive users (users who are logged on locally to the machine), no special group membership is required to execute the NetShareEnum() function. For non-interactive users, Administrator, Power User, Print Operator, or Server Operator group membership is required to successfully execute the NetShareEnum() function at levels 2, 502, and 503. No special group membership is required for level 0 or level 1 calls. For Windows Server 2003, Windows XP, Windows 2000 Server, and Windows 2000 Professional:  For all users, Administrator, Power User, Print Operator, or Server Operator group membership is required to successfully execute the NetShareEnum() function at levels 2 and 502.

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.

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. Windows Server 2003 and Windows XP:  If you call this function at information level 2 or 502 on a domain controller that is running Active Directory, access is allowed or denied based on the ACL for the securable object. To enable anonymous access, the user Anonymous must be a member of the Pre-Windows 2000-Compatible Access Group. This is because anonymous tokens do not include the Everyone group SID by default. If you call this function at information level 2 or 502 on a member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the EveryoneIncludesAnonymous() policy setting allows anonymous access.

Windows 2000 Server and Windows 2000 Professional:  If you call this function at information level 2 or 502 on a 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 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 or 502 on a member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the RestrictAnonymous() policy setting allows anonymous access.

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.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetShareEnum() Program Example: Creating new Win32 C++ console application project

Then, add the source file and give it a suitable name.

 

NetShareEnum() Program Example: Adding new C++ source file to the existing Visual C++ project

 

Then, add the following source code.

 

#include <windows.h>

#include <stdio.h>

#include <lm.h>

 

#pragma comment(lib, "Netapi32.lib")

 

int wmain(int argc, WCHAR *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[1];

      break;

   default:

      wprintf(L"Usage: %s <servername>\n", lpszArgv[0]);

      return 1;

   }

 

   // Print a report header.

   wprintf(L"Share               Local Path                           Uses    Descriptor\n");

   wprintf(L"------------------------------------------------------------------------------\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;

 

         wprintf(L"NetShareEnum() is OK!\n");

 

         // Loop through the entries;

         //  print retrieved data.

         for(i=1;i<=er;i++)

         {

            wprintf(L"%-20s%-45s%-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))

               wprintf(L"Yes\n");

            else

               wprintf(L"No\n");

            p++;

         }

 

         // Free the allocated buffer.

         NetApiBufferFree(BufPtr);

      }

      else

        {

              wprintf(L"NetShareEnum() failed! Error: %ld\n", res);

        }

   }

   // Continue to call NetShareEnum() while there are more entries.

   while (res==ERROR_MORE_DATA); // end do

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

NetShareEnum() Program Example: A sample output showing all the shared folder in the local pc, programmatically

 

 

 

 

< Win32 Network Management APIs 41 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 43 >