Adding a local user with a password
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
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 or use #pragma comment(lib, "netapi32.lib")
To do: Adding a local user with password
To show: The windows user management functions
//********* cplus.cpp **********
// For WinXp
#define _WIN32_WINNT 0x0501
// Wide character/Unicode based program
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#include <lm.h>
// Creating new user account with password...
// This program accept 3 arguments: servername, username and password.
// It is run on local WinXp machine so the servername is the
// local WinXp machine name or you can use NULL for the 1st parameter
// of the NetUserAdd() and arguments, should be without the servername.
int wmain(int argc, wchar_t *argv[])
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
if(argc != 4)
{
fwprintf(stderr, L"Usage: %s ServerName UserName Password.\n", argv[0]);
fwprintf(stderr, L"For example: %s mypersonal \"sonofbitch\" xyz123.\n", argv[0]);
// or use fwprintf(stderr, L"Usage: %s UserName Password.\n", argv[0]);
// for local machine and adjust other argc and argv[] 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
ui.usri1_priv = USER_PRIV_USER; // As a normal/restricted user
ui.usri1_home_dir = NULL; // No home directory
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 = NULL; // No script path
// 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;
}
Output example:
(This program run at the command prompt)
F:\vc2005project\cplus\debug>cplus
Usage: cplus ServerName UserName Password.
For example: cplus mypersonal "sonofbitch" xyz123.
F:\vc2005project\cplus\debug>cplus mypersonal sonofgood 123xyz
sonofgood user has been successfully added on mypersonal machine.
Username: sonofgood password: 123xyz.
F:\vc2005project\cplus\debug>
Verify the group membership addition. For Win Xp Pro SP2, click Start->Programs->Administrative Tools->Computer Management->Expand the System Tools folder on the left pane->Expand the Local Users and Groups->Click the Groups subfolder->On the right pane, select and right click 'mytestgrp' group and select the Properties context menu. You should see the 'testuser' user has been added as a member of the group. Test also for the domain group.