Adding a domain user account from another domain member server

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2 and 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 account from another domain member server programmatically

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

 

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

// For win 2003 as a target, change accordingly

#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, user account and password.

// It is run locally on Win 2000 DC of kpts.org domain. The DC server name is tutorkpts. // May be NULL if it is run on local...

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

{

USER_INFO_1 ui;

DWORD dwLevel = 1;

DWORD dwError = 0;

NET_API_STATUS nStatus;

if(argc != 4)

{

// For NT40 need to append \\Servername

fwprintf(stderr, L"Usage: %s ServerName UserName Password.\n", argv[0]);

fwprintf(stderr, L"Example: %s mypersonal Caroline abc123.\n", argv[0]);

// or use fwprintf(stderr, L"Usage: %s UserName Password.\n", argv[0]);

// for local machine and adjust other array element appropriately.

exit(1);

}

// Set up the USER_INFO_1 structure.

// USER_PRIV_USER: name identifies an normal user

// UF_SCRIPT: required for LAN Manager 2.0 and Windows NT and later.

ui.usri1_name = argv[2]; // Username entered through command line

ui.usri1_password = argv[4]; // Password, through command line

ui.usri1_priv = USER_PRIV_USER; // As a normal/restricted user

ui.usri1_home_dir = L""; // No home directory, just dummy or point it to NULL

ui.usri1_comment = L"This is a test normal user account using NetUserAdd()"; // Comment

ui.usri1_flags = UF_SCRIPT; // Must be UF_SCRIPT

ui.usri1_script_path = L""; // No script path, just dummy or point it to NULL

// Call the NetUserAdd() function, specifying level 1.

nStatus = NetUserAdd(argv[1],

dwLevel,

(LPBYTE)&ui,

&dwError);

// If the call succeeds, inform the user.

if(nStatus == NERR_Success)

{

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

fwprintf(stderr, L"Username: %s password: %s.\n", argv[2], argv[3]);

}

// Otherwise, print the system error.

else

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

return 0;

}

// If the function succeeds, the return value is NERR_Success.

// If the function fails, the return value can be one of the following error codes.

//

// ERROR_ACCESS_DENIED - The user does not have access to the requested information.

// NERR_InvalidComputer - The computer name is invalid.

// NERR_NotPrimary - The operation is allowed only on the primary domain controller of the domain.

// NERR_GroupExists - The group already exists.

// NERR_UserExists - The user account already exists.

// NERR_PasswordTooShort - The password is shorter than required. (The password could also

// be too long, be too recent in its change history, not have enough

// unique characters, or not meet another password policy requirement). It is 2245 error code.

 

 

Output when tested on Windows 2003 Server (standard Edition) domain member server. mail is another Windows 2003 Server (standard Edition) and it is a Domain Controller (DC). Error lor!!!

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus ServerName UserName Password.

Example: cplus mypersonal Caroline abc123.

 

F:\vc2005project\cplus\debug>cplus mail Caroline a!#$@b123cd

A system error has occurred: 2245

 

F:\vc2005project\cplus\debug>

 

Output when tested on Windows XP SP2 local machine. mypersonal is the computer name.

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus ServerName UserName Password.

Example: cplus mypersonal Caroline abc123.

 

F:\vc2005project\cplus\debug>cplus mypersonal Caroline abc123

Caroline user has been successfully added on mypersonal machine.

Username: Caroline password: abc123.

 

F:\vc2005project\cplus\debug>cplus mypersonal Josephine xyz543

Josephine user has been successfully added on mypersonal machine.

Username: Josephine password: xyz543.

 

F:\vc2005project\cplus\debug>

 

 

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