NetUserSetGroups() Program Example
The NetUserSetGroups() function sets global group memberships for a specified user account. The following code sample demonstrates how to set global group memberships for a user account with a call to the NetUserSetGroups() function. The code sample fills in the grui0_name member of the GROUP_USERS_INFO_0 structure and calls NetUserSetGroups(), specifying information level 0.
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 <windows.h>
#include <lm.h>
int wmain(int argc, wchar_t *argv[])
{
DWORD dwLevel = 0;
GROUP_USERS_INFO_0 gi;
NET_API_STATUS nStatus;
if (argc != 4)
{
fwprintf_s(stderr, LUsage: %s \\\\ServerName UserName GroupName\n, argv[0]);
wprintf(LExample: %s \\\\BigGuySer \Mike Spoon\ Administrators\n, argv[0]);
exit(1);
}
// Fill in the GROUP_USERS_INFO_0 structure member.
gi.grui0_name = argv[3];
// Call the NetUserSetGroups() function; specify level 0.
nStatus = NetUserSetGroups(argv[1],argv[2],dwLevel,(LPBYTE)&gi,1);
// If the call succeeds, inform the user.
if (nStatus == NERR_Success)
{
wprintf(LNetUserSetGroups() looks pretty fine!\n);
fwprintf_s(stderr, LGroup membership has been successful for %s\n, argv[2]);
}
// Otherwise, print the system error.
else
{
wprintf(LNetUserSetGroups() failed miserably!\n);
fwprintf_s(stderr, LA system error has occurred: %d\n, nStatus);
}
return 0;
}
Build and run the project. The following screenshot is a sample output.