The Win32 Network Management APIs 37

 

 

 

 

 

NetUserGetLocalGroups() Program Example

 

The NetUserGetLocalGroups() function retrieves a list of local groups to which a specified user belongs. The following code sample demonstrates how to retrieve a list of the local groups to which a user belongs with a call to the NetUserGetLocalGroups() function. The sample calls NetUserGetLocalGroups(), specifying information level 0 (LOCALGROUP_USERS_INFO_0). The sample loops through the entries and prints the name of each local group in which the user has membership. If all available entries are not enumerated, it also prints the number of entries actually enumerated and the total number of entries available. 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.

 

NetUserGetLocalGroups() Program Example: Creating new Win32 C++ console application project using Visual C++ .NET

 

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

 

NetUserGetLocalGroups() Program Example: Adding new C++ source file for the C++ source code

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

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

 

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

 

int wmain(int argc, wchar_t *argv[])

{

   LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;

   DWORD dwLevel = 0;

   DWORD dwFlags = LG_INCLUDE_INDIRECT ;

   DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

   DWORD dwEntriesRead = 0;

   DWORD dwTotalEntries = 0;

   NET_API_STATUS nStatus;

   LPLOCALGROUP_USERS_INFO_0 pTmpBuf;

   DWORD i;

   DWORD dwTotalCount = 0;

 

   if (argc != 3)

   {

      fwprintf_s(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);

      wprintf(L"Example: %s \\\\AtlanticSvr JohnBye\n", argv[0]);

      exit(1);

   }

 

   // Call the NetUserGetLocalGroups() function

   //  specifying information level 0.

   //

   //  The LG_INCLUDE_INDIRECT flag specifies that the

   //   function should also return the names of the local

   //   groups in which the user is indirectly a member.

   nStatus = NetUserGetLocalGroups(argv[1],

                                   argv[2],

                                   dwLevel,

                                   dwFlags,

                                   (LPBYTE *) &pBuf,

                                   dwPrefMaxLen,

                                   &dwEntriesRead,

                                   &dwTotalEntries);

   // If the call succeeds,

   if (nStatus == NERR_Success)

   {

      wprintf(L"NetUserGetLocalGroups() looks OK!\n");

      if ((pTmpBuf = pBuf) != NULL)

      {

         fwprintf_s(stderr, L"\nLocal group(s):\n");

         // Loop through the entries and

         //  print the names of the local groups

         //  to which the user belongs.

         for (i = 0; i < dwEntriesRead; i++)

         {

            assert(pTmpBuf != NULL);

 

            if (pTmpBuf == NULL)

            {

               fwprintf_s(stderr, L"An access violation has occurred\n");

               break;

            }

 

            wprintf(L"\t%d. %s\n", i, pTmpBuf->lgrui0_name);

 

            pTmpBuf++;

            dwTotalCount++;

         }

      }

      // If all available entries were not enumerated, print the number actually

      //  enumerated and the total number available.

      if (dwEntriesRead < dwTotalEntries)

         fwprintf_s(stderr, L"\nTotal entries: %d", dwTotalEntries);

      // Otherwise, just print the total.

      wprintf(L"\nEntries enumerated: %d\n", dwTotalCount);

   }

   else

   {

         wprintf(L"NetUserGetLocalGroups() failed!\n");

         fwprintf_s(stderr, L"A system error has occurred: %d\n", nStatus);

   }

   // Free the allocated memory.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

 

   return 0;

}

 

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

 

NetUserGetLocalGroups() Program Example: A sample program output

 

 

 

 

< Win32 Network Management APIs 36 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 38 >