The Win32 Network Management APIs 22

 

 

 

 

 

NetUserGetGroups() Program Example

 

The NetUserGetGroups() function retrieves a list of global groups to which a specified user belongs. The following code sample demonstrates how to retrieve a list of global groups to which a user belongs with a call to the NetUserGetGroups() function. The sample calls NetUserGetGroups(), specifying information level 0 (GROUP_USERS_INFO_0). The code loops through the entries and prints the name of the global groups in which the user has membership. The sample also prints the total number of entries that are available and the number of entries actually enumerated if they do not match. Finally, the code sample frees the memory allocated for the buffer.

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

 

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

 

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

 

NetUserGetGroups() Program Example: Adding new C++ source file for the 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[])

{

      LPGROUP_USERS_INFO_0 pBuf = NULL;

      DWORD dwLevel = 0;

      DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

      DWORD dwEntriesRead = 0;

      DWORD dwTotalEntries = 0;

      NET_API_STATUS nStatus;

      DWORD i;         

      DWORD dwTotalCount = 0;

      LPGROUP_USERS_INFO_0 pTmpBuf;

     

      if (argc != 3)

      {

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

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

            exit(1);

      }

     

      // Call the NetUserGetGroups() function, specifying level 0.

      nStatus = NetUserGetGroups(argv[1],argv[2],dwLevel,(LPBYTE*)&pBuf,dwPrefMaxLen,&dwEntriesRead,&dwTotalEntries);

     

      // If the call succeeds,

      if (nStatus == NERR_Success)

      {

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

           

            if ((pTmpBuf = pBuf) != NULL)

            {

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

                 

                  // Loop through the entries;

                  //  print the name of the global 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-- %s\n", pTmpBuf->grui0_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"NetUserGetGroups() looks OK!\n");

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

      }

     

      // Free the allocated buffer.

      if (pBuf != NULL)

            NetApiBufferFree(pBuf);

      return 0;

}

 

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

 

NetUserGetGroups() Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 21 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 23 >