The Win32 Network Management APIs 18

 

 

 

 

 

Creating a Local Group and Adding a User Program Example

 

To create a new local group, call the NetLocalGroupAdd() function. To add a user to that group, call the NetLocalGroupAddMembers() function. The following program allows you to create a user and a local group and add the user to the local group.

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

 

Creating a Local Group and Adding a User Program Example: Creating new Win32 console application project in Visual C++

 

Then, add the source file. Give it a name.

 

Creating a Local Group and Adding a User Program Example: Adding new C++ source file

 

Then, add the following source code.

 

#include <windows.h>

#include <lmcons.h>

#include <lmaccess.h>

#include <lmerr.h>

#include <lmapibuf.h>

#include <stdio.h>

#include <stdlib.h>

 

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

 

NET_API_STATUS NetSample(LPWSTR lpszDomain,LPWSTR lpszUser,LPWSTR lpszPassword,LPWSTR lpszLocalGroup)

{

    USER_INFO_1               user_info;

    LOCALGROUP_INFO_1         localgroup_info;

    LOCALGROUP_MEMBERS_INFO_3 localgroup_members;

    LPWSTR                    lpszPrimaryDC = NULL;

    NET_API_STATUS            err = 0;

    DWORD                     parm_err = 0;

     

    // First get the name of the primary domain controller.

    // Be sure to free the returned buffer.

    err = NetGetDCName(NULL,                    // local computer

                   lpszDomain,                                 // domain name

                   (LPBYTE *)&lpszPrimaryDC);   // returned PDC

 

    if (err != 0)

    {

        wprintf(L"Error getting DC name, error %d\n", err);

        return(err);

    }

      else

            wprintf(L"%s found!\n", lpszDomain);

     

    // Set up the USER_INFO_1 structure.

    user_info.usri1_name = lpszUser;

    user_info.usri1_password = lpszPassword;

    user_info.usri1_priv = USER_PRIV_USER;

    user_info.usri1_home_dir = L"";

    user_info.usri1_comment = L"Sample User";

    user_info.usri1_flags = UF_SCRIPT;

    user_info.usri1_script_path = TEXT("");

 

    err = NetUserAdd(lpszPrimaryDC,        // PDC name

                      1,                                              // level

                      (LPBYTE) &user_info,           // input buffer

                      &parm_err );                           // parameter in error

 

    switch (err)

    {

    case 0:

        wprintf(L"User successfully created.\n");

        break;

    case NERR_UserExists:

        wprintf(L"User already exists.\n");

        err = 0;

        break;

    case ERROR_INVALID_PARAMETER:

        wprintf(L"Invalid parameter error adding user; parameter index = %d\n", parm_err);

        NetApiBufferFree( lpszPrimaryDC );

        return(err);

    default:

        wprintf(L"Error adding user: %d\n", err);

        NetApiBufferFree(lpszPrimaryDC);

        return(err);

    }

     

    // Set up the LOCALGROUP_INFO_1 structure.

    localgroup_info.lgrpi1_name = lpszLocalGroup;

    localgroup_info.lgrpi1_comment = L"Sample local group.";

 

    err = NetLocalGroupAdd( lpszPrimaryDC,    // PDC name

                  1,                                                           // level

                  (LPBYTE) &localgroup_info,             // input buffer

                  &parm_err);                                         // parameter in error

 

    switch(err)

    {

    case 0:

        wprintf(L"Local group successfully created.\n");

        break;

    case ERROR_ALIAS_EXISTS:

        wprintf(L"Local group already exists.\n");

        err = 0;

        break;

    case ERROR_INVALID_PARAMETER:

        wprintf(L"Invalid parameter error adding local group; parameter index = %d\n", err, parm_err);

        NetApiBufferFree(lpszPrimaryDC);

        return(err);

    default:

        wprintf(L"Error adding local group: %d\n", err);

        NetApiBufferFree( lpszPrimaryDC );

        return( err );

    }

     

    // Now add the user to the local group.

    localgroup_members.lgrmi3_domainandname = lpszUser;

 

    err = NetLocalGroupAddMembers(lpszPrimaryDC,         // PDC name

                           lpszLocalGroup,    // group name

                           3,                            // name

                           (LPBYTE) &localgroup_members, // buffer

                           1);                           // count

 

    switch (err)

    {

    case 0:

        wprintf(L"User successfully added to local group.\n");

        break;

    case ERROR_MEMBER_IN_ALIAS:

        wprintf(L"User already in local group.\n");

        err = 0;

        break;

    default:

        wprintf(L"Error adding user to local group: %d\n", err);

        break;

    }

 

    NetApiBufferFree(lpszPrimaryDC);

    return(err);

}

 

int wmain(int cArgs, char * pArgs[])

{

    NET_API_STATUS err = 0;

 

    wprintf(L"Calling NetSample...\n");

    // Change the following info accordingly...

    err = NetSample(L"SampleDomainName",L"SampleUserName",L"SampleUserPswd",L"SampleLocalGroup");

    wprintf(L"NetSample returned %d\n", err);

    return(0);

}

 

Build and run the project. The following screenshot is a sample output. This program must be run in the domain based network.

 

Creating a Local Group and Adding a User Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 17 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 19 >