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.

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

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, LUsage: %s \\\\ServerName UserName\n, argv[0]);
wprintf(LExample: %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(LNetUserGetLocalGroups() 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, LAn 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(LNetUserGetLocalGroups() failed!\n);
fwprintf_s(stderr, LA 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.
