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.