Creating the Windows a user and a group then add the user to the group which was done at domain member server

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server (standard Edition)

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Create a user and a group then add the user to the group. Done at domain member server

To show: More on various Windows domain user and group management C functions

 

//********* cplus.cpp ************

// Run on the domain machine

// Network management functions have their own error codes

// Target for win 2003

#define WIN32_WINNT 0x0502

 

#define UNICODE 1

 

#include <windows>

#include <lmcons.h>

#include <lmaccess.h>

#include <lmerr.h>

#include <lmapibuf.h>

#include <stdio.h>

#include <stdlib.h>

 

NET_API_STATUS MyTestNet(LPWSTR lpszDomain,

LPWSTR lpszUser,

LPWSTR lpszPassword,

LPWSTR lpszLocalGroup )

{

USER_INFO_1 user_info;

LOCALGROUP_INFO_1 localgroup_info;

LOCALGROUP_MEMBERS_INFO_3 localgroup_members;

// Change the primary DC accordingly

LPWSTR lpszPrimaryDC = L"mail";

NET_API_STATUS err = 0;

DWORD parm_err = 0;

 

// First get the name of the primary domain controller. Make sure to free the returned buffer.

err = NetGetDCName(L"mail", // Local machine name/domain name. Change accordingly

lpszDomain, // Domain name, if NULL use lpszPrimaryDC

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

 

if(err != 0)

{

printf("Error getting DC name: %d\n", err);

return(err);

}

 

// 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 = TEXT("");

user_info.usri1_comment = TEXT("This is just a sample user lol!");

user_info.usri1_flags = UF_SCRIPT;

user_info.usri1_script_path = TEXT("");

err = NetUserAdd(lpszPrimaryDC, // PDC name

1, // Level, use other level for more information

(LPBYTE)&user_info, // Input buffer

&parm_err); // Parameter in error

 

switch (err)

{

case 0:

printf("%ls user successfully created.\n", user_info.usri1_name);

break;

case NERR_UserExists:

printf("%ls user already exists.\n", user_info.usri1_name);

err = 0;

break;

case ERROR_INVALID_PARAMETER:

{

printf("Invalid Parameter Error adding user: Parameter Index = %d\n", parm_err);

NetApiBufferFree(lpszPrimaryDC);

return(err);

}

default:

printf("Error adding %ls user: %d\n", user_info.usri1_name, err);

NetApiBufferFree(lpszPrimaryDC);

return(err);

}

// Set up the LOCALGROUP_INFO_1 structure.

localgroup_info.lgrpi1_name = lpszLocalGroup;

localgroup_info.lgrpi1_comment = TEXT("This is just a sample Local group.");

err = NetLocalGroupAdd(lpszPrimaryDC, // PDC name

1, // Level

(LPBYTE)&localgroup_info, // Input buffer

&parm_err); // Parameter in error

 

switch (err)

{

case 0:

printf("%ls Local Group successfully created.\n", localgroup_info.lgrpi1_name);

break;

case ERROR_ALIAS_EXISTS:

printf("%ls Local Group already exists.\n", localgroup_info.lgrpi1_name);

err = 0;

break;

case ERROR_INVALID_PARAMETER:

{

printf("Invalid Parameter Error adding Local Group: Parameter Index = %d\n", err, parm_err);

NetApiBufferFree(lpszPrimaryDC);

return(err);

}

default:

printf("Error adding %ls Local Group: %d\n", localgroup_info.lgrpi1_name, 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:

printf("%ls user successfully added to %ls Local Group.\n", user_info.usri1_name, localgroup_info.lgrpi1_name);

break;

case ERROR_MEMBER_IN_ALIAS:

printf("User %ls already in %ls Local Group.\n", user_info.usri1_name, localgroup_info.lgrpi1_name);

err = 0;

break;

default:

printf("Error adding %ls user to %ls Local Group: %d\n", user_info.usri1_name, localgroup_info.lgrpi1_name, err);

break;

}

NetApiBufferFree(lpszPrimaryDC);

return (err);

}

 

// This program run at command prompt, receives 4 arguments: The domain name, user name (user account), user password and the group name.

int wmain(int argc, wchar_t *argv[])

{

NET_API_STATUS err = 0;

 

if(argc != 5)

{

printf("Usage: %ls <domain_name> <user_name> <password> <group_name>\n", argv[0]);

printf("Example: %ls mydomain testuser1 123abc mytestgrp\n", argv[0]);

exit (-1);

}

printf("Calling MyTestNet(): Create a user and a group then,\n");

printf("add the user to the group.\n");

printf("===================================================.\n");

err = MyTestNet(argv[1], // domain name

argv[2], // user account

argv[3], // password for the user

argv[4]); // group name

printf("MyTestNet() returned %d\n", err);

return (0);

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus <domain_name> <user_name> <password> <group_name>

Example: cplus mydomain testuser1 123abc mytestgrp

 

F:\vc2005project\cplus\debug>cplus test.org testuser2 abc123$#rs mytestgroup

Calling MyTestNet(): Create a user and a group then,

add the user to the group.

===================================================.

Error getting DC name: 53

MyTestNet() returned 53

 

F:\vc2005project\cplus\debug>cplus test.org testuser2 abc123$#rs mytestgroup

Calling MyTestNet(): Create a user and a group then,

add the user to the group.

===================================================.

Error getting DC name: 2453

MyTestNet() returned 2453

 

F:\vc2005project\cplus\debug>cplus test testuser2 abc123$#rs mytestgroup

Calling MyTestNet(): Create a user and a group then,

add the user to the group.

===================================================.

testuser2 user successfully created.

mytestgroup Local Group successfully created.

testuser2 user successfully added to mytestgroup Local Group.

MyTestNet() returned 0

 

F:\vc2005project\cplus\debug>cplus test testuser3 abc123$#rs mytestgroup1

Calling MyTestNet(): Create a user and a group then,

add the user to the group.

===================================================.

testuser3 user successfully created.

mytestgroup1 Local Group successfully created.

testuser3 user successfully added to mytestgroup1 Local Group.

MyTestNet() returned 0

 

F:\vc2005project\cplus\debug>

 

Note: mail is a Domain Controller (DC) of a Windows 2003 domain. The program was run on another Windows 2003 member server. The login is local Administrator. test.org is the domain name.

 

 

C and C++ Programming Resources | C & C++ Code Example Index