Adding a domain user to the domain Administrators group through the domain member server

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server, Standard Edition and tested on the same server. It is a member server of the Windows 2003 domain.

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 Administrators group through the domain member server programmatically

To show: Various domain user and group management C Win32 functions

 

 

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

// For Win 2000

// #define _WIN32_WINNT 0x0500

// For Win xp

// #define _WIN32_WINNT 0x0501

// For Win 2003

#define _WIN32_WINNT 0x0502

 

// Wide character/Unicode based program

#ifndef UNICODE

#define UNICODE

#endif

 

#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[])

{

LOCALGROUP_MEMBERS_INFO_3 lgmi3;

DWORD dwLevel = 3;

DWORD totalEntries = 1;

NET_API_STATUS nStatus;

 

if(argc != 4)

{

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

fwprintf(stderr, L"Example: %s mypersonal mytestgroup \\yahoo.com\\Administrators-(DomainName\\AccountName)\n", argv[0]);

// Just exit, no further processing

exit(1);

}

// 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],

argv[2],

dwLevel,

(LPBYTE)&lgmi3,

totalEntries);

// 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.

else

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

return 0;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

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

Example: cplus mypersonal mytestgroup \yahoo.com\Administrators-(DomainName\AccountName)

 

F:\vc2005project\cplus\debug>cplus mail Administrators jmti.gov.my\modadmin

test.org\modadmin has been added successfully to Administrators group on mail machine.

 

F:\vc2005project\cplus\debug>cplus mail Administrators jmti.gov.my\test1

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

 

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