How to add user to system programmatically?

29 07 2008


I still remember, during my first computer course( DOS, Windows 3.1 and BASIC) the machine login names provided were like this – s1,s2… etc. I think they’ve used some kind of scripts to generate login for whole 50 students. if they create all users manually, it might take a lot of time.

Well, now I’m grown up and just thought about those old days – How they might added the users by script? Can i do the same in my modern windows box programmatically?


You can use the api – NetUserAdd(). See the code snippet.

#include "Lm.h"
...
// New User information.
USER_INFO_1 UserInfo;
UserInfo.usri1_name = L"WeSeeTips";       // User Name
UserInfo.usri1_password = L"ThatsSecret"; // Password.

UserInfo.usri1_priv = USER_PRIV_USER; // Normal User.
UserInfo.usri1_flags = UF_SCRIPT;

UserInfo.usri1_home_dir = 0;    // Home Directory.
UserInfo.usri1_comment = 0;     // User comment.
UserInfo.usri1_script_path = 0; // Path of script.

// Add the new user.
DWORD Error = 0;
NetUserAdd( 0,  // Local machine.
            1,  // User info level.
            (BYTE*)&UserInfo, // User info.
            &Error ); // Error code.


Well, don’t forget to add Netapi32.lib to project settings.


Targeted Audience – Intermediate.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: