Create a remote user using the NetUserAdd API function

Source: Internet
Author: User

http://apps.hi.baidu.com/share/detail/33407620

Creating a remote user with NetUserAdd programming
Windows API NetUserAdd () can create Windows users, whether local or remote.

Net_api_status NetUserAdd (
LMSTR servername,
DWORD level,
Lpbyte buf,
Lpdword Parm_err
);

ServerName
[In] Pointer to a constant string this specifies the DNS or NetBIOS name of the remote server on which the function was to Execu Te. If This parameter are NULL, the local computer is used.

Although MSDN says, there is a problem: if the current logged-on user does not have administrator privileges on the remote machine, creating a remote user will fail. For example, the current user is a user administrator on host A and wants to be able to create a new user testb on Host B, apparently the user hosta\administrator is not the user of the Administrators group on Host B, Therefore, creation will fail due to security issues. We must provide a user name and password with administrator privileges on Windows Host B, such as hostb\administrator, but even so netuseradd () How do I tell Windows?

I thought about impersonate functions, but such as Impersonatelogonuser () do not impersonate a remote user. In the foreign site can not find similar example, is helpless, suddenly think of a try to let host A and Host B to establish a network connection, that is, call WNetAddConnection2 () to establish a network map, and then call NetUserAdd (), the result actually succeeded!

The code is as follows:

Netresource nr;

Memset (&nr,0,sizeof (NR));
Nr.dwtype = Resourcetype_disk;
Nr.lplocalname = "X:";
Nr.lpremotename = "\\\\sean01\\d$\\test";

DWORD Dwret = WNetAddConnection2 (&NR, "password", "Sean01\\administrator", connect_update_profile);

User_info_1 NewUser;

memset (&newuser,0,sizeof (NewUser));

Newuser.usri1_name = L "Usertestone"; Allocates the username
Newuser.usri1_password = L "abcd1234"; Allocates the password
Newuser.usri1_priv = 1; Sets the account type to User_priv_user
Newuser.usri1_home_dir = NULL; We didn ' t supply a Home Directory
Newuser.usri1_comment = L "Create remote user"; Comment on the User
Newuser.usri1_script_path = NULL; We didn ' t supply a Logon Script Path

Dwret = NetUserAdd (L "\\\\sean01", 1, (LPBYTE) &newuser, 0);
if (Dwret! = 0)//If the call fails we get a Non-zero value
{
MessageBox (NULL, "Error Adding User", null,null);
}
Else
MessageBox (NULL, "CreateUser OK", null,null);

Although not the best way, but can be used. I hope one day I can know how to use NetUserAdd () can be done.

Http://www.cnblogs.com/-clq/archive/2012/02/23/2364559.html

Create a remote user using the NetUserAdd API function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.