The Win32 Network Management APIs 38

 

 

 

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.

 

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

 

 

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

 

NetUserSetGroups() 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 <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.

 

NetUserSetGroups() Program Example: A s ample program output

 

 

 

< Win32 Network Management APIs 37 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 39 >