如何使用netuseradd在WINNT中添加用户(100分)

H

hxchxc

Unregistered / Unconfirmed
GUEST, unregistred user!
要求有例程。我自己写了一个但是有问题。
...
type USER_INFO_1=record
usri1_name:pchar;
usri1_password:pchar;
usri1_password_age:dword;
usri1_priv:dword;
usri1_home_dir:pchar;
usri1_comment:pchar;
usri1_flags:dword;
usri1_script_path:pchar;
end;
buffer=^USER_INFO_1;
...
function NetUserAdd(Server:pWideChar;Level:DWORD;Buf:buffer;ParmError:pChar):LongInt;
stdcall; external 'netapi32.dll'

procedure TForm1.Button1Click(Sender: TObject);
var buf:buffer;
error:pchar;
begin
getmem(buf,sizeof(USER_INFO_1));
with buf^ do
begin
usri1_name:='guest';
usri1_password:='123';
usri1_password_age:=20000;
usri1_priv:=1;
usri1_home_dir:=nil;
usri1_comment:=nil;
usri1_flags:=0;
usri1_script_path:=nil;
end;
showmessage(inttostr(netuseradd(nil,2,buf,nil)));
showmessage(string(error));
freemem(buf);
end;
 
为什么不用ADSi来管理用户呢?
 
你指的的是NT管理工具的吧,如果有1000个用户要加,这个工作量也太大了。
 
以下这段代码是我的程序中用到的,是用c++builder写的,你改一下就是了.

String comment="单位:"+userproperty->userunit->Text
+",电话:"+userproperty->phone->Text;
String fullname=userproperty->username->Text+"("+
userproperty->identitycard->Text+")";

LPWSTR lpszDomain=new WideChar[userproperty->domain->Text.WideCharBufSize()];
LPWSTR lpszUser=new WideChar[userproperty->loginname->Text.WideCharBufSize()];
LPWSTR lpszPassword=new WideChar[userproperty->loginpassword->Text.WideCharBufSize()];
LPWSTR lpszComment=new WideChar[comment.WideCharBufSize()];
LPWSTR lpszFullname=new WideChar[fullname.WideCharBufSize()];

lpszDomain=userproperty->domain->Text.WideChar(
lpszDomain,userproperty->domain->Text.WideCharBufSize());
lpszUser=userproperty->loginname->Text.WideChar(
lpszUser,userproperty->loginname->Text.WideCharBufSize());
lpszPassword=userproperty->loginpassword->Text.WideChar(
lpszPassword,userproperty->loginpassword->Text.WideCharBufSize());
lpszComment=comment.WideChar(lpszComment,comment.WideCharBufSize());
lpszFullname=fullname.WideChar(lpszFullname,fullname.WideCharBufSize());


DWORD Property=UF_SCRIPT;
switch(userproperty->accountstate->ItemIndex)
{
case 0:user_info.usri3_password_expired=TRUE;//下次登录时更改密码
break;
case 1: Property=UF_SCRIPT|UF_DONT_EXPIRE_PASSWD; //密码永不过期
break;
case 2: Property=UF_SCRIPT|UF_PASSWD_CANT_CHANGE; //用户不得更改密码
break;
case 3: Property=UF_SCRIPT|UF_ACCOUNTDISABLE;
break; //帐号禁用
}

user_info.usri3_name = lpszUser;
user_info.usri3_password = lpszPassword;
user_info.usri3_priv = USER_PRIV_USER;
user_info.usri3_home_dir =NULL;
user_info.usri3_comment =lpszComment
user_info.usri3_flags =Property;
user_info.usri3_script_path =NULL;
user_info.usri3_full_name=lpszFullname;
user_info.usri3_auth_flags=0;
user_info.usri3_logon_hours=NULL;
user_info.usri3_usr_comment=lpszComment;
user_info.usri3_acct_expires=TIMEQ_FOREVER;
user_info.usri3_max_storage=USER_MAXSTORAGE_UNLIMITED;
user_info.usri3_country_code=860;
user_info.usri3_code_page=936;
user_info.usri3_parms=NULL;
user_info.usri3_workstations=NULL;
user_info.usri3_home_dir_drive=NULL;
user_info.usri3_primary_group_id=DOMAIN_GROUP_RID_USERS;
user_info.usri3_profile=NULL;

NET_API_STATUS err=NetAddUser(lpszDomain,user_info);
 
这个东西我已经自己解决了。以下是我的源程序,大家可以看看。
type USER_INFO_1=record
usri1_name:pwidechar;
usri1_password:pwidechar;
usri1_password_age:dword;
usri1_priv:dword;
usri1_home_dir:pwidechar;
usri1_comment:pwidechar;
usri1_flags:dword;
usri1_script_path:pwidechar;
end;
buffer=^USER_INFO_1;
.....
procedure TForm1.Button1Click(Sender: TObject);
var buf:buffer;
error:pchar;
begin
getmem(buf,sizeof(USER_INFO_1));
with buf^ do
begin
usri1_name:='123e';
usri1_password:='123456789';
usri1_password_age:=0;
usri1_priv:=1;
usri1_home_dir:=nil;
usri1_comment:=nil;
usri1_flags:=1;
usri1_script_path:=nil;
end;
//netuseradd(nil,1,pointer(buf),
showmessage(inttostr(netuseradd(nil,1,pointer(buf),0)));
freemem(buf);
end;
 
多人接受答案了。
 
顶部