Use netuseradd to program the creation of remote users

Source: Internet
Author: User

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

 

Use netuseradd to program the creation of remote users
Windows API netuseradd () allows you to create Windows users, both local and remote.

Net_api_status netuseradd (
Lmstr servername,
DWORD level,
Lpbyte Buf,
Lpdword parm_err
);

Servername
[In] pointer to a constant string that specifies the DNS or NetBIOS Name of the remote server on which the function is to execute. If this parameter is null, the local computer is used.

Although msdn says, there is a problem: if the current login user does not have the Administrator permission on the remote machine, the creation of the remote user will fail. For example, the current user is the user administrator on host a and wants to create a new user testb on host B. Apparently, the user Hosta \ administrator is not a user in the Administrators group on host B, therefore, the creation will fail due to security issues. We must provide a user name and password with administrator permissions on Windows Host B, such as hostb \ administrator. But even so, how does netuseradd () Tell windows?

I have thought about impersonate and other functions, but impersonatelogonuser () cannot simulate remote users. Similar examples could not be found on foreign websites. when there was no problem, I suddenly thought of trying to set up a network connection between host a and host B, that is, calling wnetaddconnection2 () create a network ing and then call netuseradd (). The result is successful!

CodeAs 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. usrisponpassword = 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. usrisponcomment = l "create remote user"; // comment on the user
Newuser. usrisponscript_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 it is not the best method, it can be used. I hope to know how to use netuseradd () someday.

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.