Adding a domain user to the domain group which is done at member server of the domain


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: Adding a domain user to the domain group. Done at the member server of the Windows domain programmatically

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





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

// For Win 2003

#define _WIN32_WINNT 0x0502


// Wide character/Unicode based program

#ifndef UNICODE

#define UNICODE



#include <windows.h>

#include <stdio.h>

#include <lm.h>


// This program accept 3 arguments: servername, GroupName and MemberAccountName (DomainName\AccountName).

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



DWORD dwLevel = 3;

DWORD totalEntries = 1;



if(argc != 4)


fwprintf(stderr, L"Usage: %s ServerName GroupName MemberAccountName-(DomainName\\AccountName)\n", argv[0]);

fwprintf(stderr, L"Usage: %s mypersonal Administrators testusername-(DomainName\\AccountName)\n", argv[0]);

// Just exit, no further processing



// Set up the LOCALGROUP_MEMBERS_INFO_3 structure. Assign the member account name in form of DomainName\AccountName

lgmi3.lgrmi3_domainandname = argv[3];

// Call the NetLocalGroupAddMembers() function, specifying level 3.

// Level 0 can use SID

nStatus = NetLocalGroupAddMembers(argv[1],





// If the call succeeds, inform the user.

if(nStatus == NERR_Success)

fwprintf(stderr, L"%s has been added successfully to %s group on %s machine.\n", argv[3], argv[2], argv[1]);

// Otherwise, print the system error.


fwprintf(stderr, L"A system error has occurred: %d\n", nStatus);

return 0;



Output example:

(This program run at the command prompt)



Usage: cplus ServerName GroupName MemberAccountName-(DomainName\AccountName)

Usage: cplus mypersonal Administrators testusername-(DomainName\AccountName)

F:\vc2005project\cplus\debug>cplus mail Administrators test1

test1 has been added successfully to Administrators group on mail machine.

F:\vc2005project\cplus\debug>cplus mail Administrators test2

test2 has been added successfully to Administrators group on mail machine.




Note: mail is a Domain Controller (DC) of a Windows 2003 domain. The program was run on another Windows 2003 member server in the similar domain. The login to the member server is local Administrator. is the domain name. Here test1 and test2 users have been successfully added to the domain Administrators group.



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