帮我看一下这代码,为何无法添加一个系统用户. ( 积分: 50 )

  • 主题发起人 主题发起人 我要学
  • 开始时间 开始时间

我要学

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

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;
function NetUserAdd(Server:PWideChar;Level:DWORD;Buf:pointer;ParmError:dword):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:='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),0);
//showmessage(inttostr(netuseradd(nil,1,pointer(buf),0)));
freemem(buf);

end;

end.


最好还说一下如何升级为管理员权限


//这此都是在DFW上找的
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

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;
function NetUserAdd(Server:PWideChar;Level:DWORD;Buf:pointer;ParmError:dword):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:='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),0);
//showmessage(inttostr(netuseradd(nil,1,pointer(buf),0)));
freemem(buf);

end;

end.


最好还说一下如何升级为管理员权限


//这此都是在DFW上找的
 
调用了'netapi32.dll'中的
function NetUserAdd(Server:PWideChar;Level:DWORD;Buf:pointer;ParmError:dword):LongInt;
stdcall;来添加用户,你有吗?!
 
关于如何升级为管理员权限问:
函数中有个Level参数,用它只能在创建时指定权限;
以后要改变权限就只能修改用户信息里的权限标志了,可能就是用户信息里的这个标志吧usri1_priv:dword;//不确定,毕竟不是我的程序
 
有啊,可能你没看清代码,
没有的话无法运行,现在是:能运行,但没有添加成功.
 
虽然问题还没解决,但还是很感谢你.ak_2005
我的系统是XP SP2 不知这个有没关系
 
我有解决的全部办法和代码,.请稍后.
 
首先用 NetUserAdd()增加一个用户,然后用NetLocalGroupAddMembers()加到Administrators组里面,也可以是其它组,这个代码肯定是对的,给分吧.

function AddSystemUser(UserName,PassWord,Memo:PWideChar):bool; {增加系统帐号函数}
var
AToken: THandle;
Priv: TTokenPrivileges;
NoUse: Cardinal;
UserInfo: _USER_INFO_1;
Members: _LOCALGROUP_MEMBERS_INFO_3;

begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, AToken);
LookupPrivilegeValue(nil, SE_SECURITY_NAME, Priv.Privileges[0].Luid);
Priv.PrivilegeCount := 1;
Priv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
//AdjustTokenPrivileges(AToken, False, Priv, 0, nil, NoUse);
//上面那段代码是为了让你的进程取得账号管理的权限。
//运行该程充的账号必须有账号管理的权限,否则会失败。
FillChar(UserInfo, Sizeof(UserInfo), 0);
UserInfo.usri1_name := UserName;
UserInfo.usri1_password := PassWord;
UserInfo.usri1_password_age := 0;
UserInfo.usri1_priv := USER_PRIV_User;
UserInfo.usri1_comment := Memo;
UserInfo.usri1_flags := UF_DONT_EXPIRE_PASSWD;
NetUserAdd(nil, 1, UserInfo, NoUse);
Members.lgrmi3_domainandname := UserName;
NetLocalGroupAddMembers(nil, 'Administrators', 3, Members, 1);

end;

调用过程:
AddSystemUser('用户名','密码','用户注释');
 
不用看也给分,不过分不多,就当交个朋友吧,

测试一下
 
改进下函数调用参数:
function AddSystemUser(UserName,PassWord,Memo:PWideChar,GroupName:String):bool; {增加系统帐号函数}
var
AToken: THandle;
Priv: TTokenPrivileges;
NoUse: Cardinal;
UserInfo: _USER_INFO_1;
Members: _LOCALGROUP_MEMBERS_INFO_3;

begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, AToken);
LookupPrivilegeValue(nil, SE_SECURITY_NAME, Priv.Privileges[0].Luid);
Priv.PrivilegeCount := 1;
Priv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
//AdjustTokenPrivileges(AToken, False, Priv, 0, nil, NoUse);
//上面那段代码是为了让你的进程取得账号管理的权限。
//运行该程充的账号必须有账号管理的权限,否则会失败。
FillChar(UserInfo, Sizeof(UserInfo), 0);
UserInfo.usri1_name := UserName;
UserInfo.usri1_password := PassWord;
UserInfo.usri1_password_age := 0;
UserInfo.usri1_priv := USER_PRIV_User;
UserInfo.usri1_comment := Memo;
UserInfo.usri1_flags := UF_DONT_EXPIRE_PASSWD;
NetUserAdd(nil, 1, UserInfo, NoUse);
Members.lgrmi3_domainandname := UserName;
NetLocalGroupAddMembers(nil, GroupName, 3, Members, 1);

end;

调用过程:
AddSystemUser('用户名','密码','用户注释','Administrators组名');
 
TO mosker
看来你还得等一下,
我现在还不会用 NetUserAdd() 增加一个用户

所以无法测试你的代码,就当我欠你个人情先.加我QQ:21288021有事随便找我,一定比得上这50分
 
晕,什么意思啊
NetUserAdd() 增加用户的功能已经包装在AddSystemUser()里面了,直接调用AddSystemUser()就可以添加用户到指定的组了.
 
忘记说了使用NetUserAdd()NetLocalGroupAddMembers()这几个函数需要在
implementation前申明下:

const
SE_CREATE_TOKEN_NAME ='SeCreateTokenPrivilege';
SE_ASSIGNPRIMARYTOKEN_NAME ='SeAssignPrimaryTokenPrivilege';
SE_LOCK_MEMORY_NAME ='SeLockMemoryPrivilege';
SE_INCREASE_QUOTA_NAME ='SeIncreaseQuotaPrivilege';
SE_UNSOLICITED_INPUT_NAME ='SeUnsolicitedInputPrivilege';
SE_MACHINE_ACCOUNT_NAME ='SeMachineAccountPrivilege';
SE_TCB_NAME ='SeTcbPrivilege';
SE_SECURITY_NAME ='SeSecurityPrivilege';
SE_TAKE_OWNERSHIP_NAME ='SeTakeOwnershipPrivilege';
SE_LOAD_DRIVER_NAME ='SeLoadDriverPrivilege';
SE_SYSTEM_PROFILE_NAME ='SeSystemProfilePrivilege';
SE_SYSTEMTIME_NAME ='SeSystemtimePrivilege';
SE_PROF_SINGLE_PROCESS_NAME ='SeProfileSingleProcessPrivilege';
SE_INC_BASE_PRIORITY_NAME ='SeIncreaseBasePriorityPrivilege';
SE_CREATE_PAGEFILE_NAME ='SeCreatePagefilePrivilege';
SE_CREATE_PERMANENT_NAME ='SeCreatePermanentPrivilege';
SE_BACKUP_NAME ='SeBackupPrivilege';
SE_RESTORE_NAME ='SeRestorePrivilege';
SE_SHUTDOWN_NAME ='SeShutdownPrivilege';
SE_DEBUG_NAME ='SeDebugPrivilege';
SE_AUDIT_NAME ='SeAuditPrivilege';
SE_SYSTEM_ENVIRONMENT_NAME ='SeSystemEnvironmentPrivilege';
SE_CHANGE_NOTIFY_NAME ='SeChangeNotifyPrivilege';
SE_REMOTE_SHUTDOWN_NAME ='SeRemoteShutdownPrivilege';

USER_PRIV_MASK =$3;
USER_PRIV_GUEST =0;
USER_PRIV_USER =1;
USER_PRIV_ADMIN =2;

UF_SCRIPT =$0001;
UF_ACCOUNTDISABLE =$0002;
UF_HOMEDIR_REQUIRED =$0008;
UF_LOCKOUT =$0010;
UF_PASSWD_NOTREQD =$0020;
UF_PASSWD_CANT_CHANGE =$0040;

//
// Account type bits as part of usri_flags.
//

UF_TEMP_DUPLICATE_ACCOUNT =$0100;
UF_NORMAL_ACCOUNT =$0200;
UF_INTERDOMAIN_TRUST_ACCOUNT =$0800;
UF_WORKSTATION_TRUST_ACCOUNT =$1000;
UF_SERVER_TRUST_ACCOUNT =$2000;

UF_MACHINE_ACCOUNT_MASK =( UF_INTERDOMAIN_TRUST_ACCOUNT or
UF_WORKSTATION_TRUST_ACCOUNT or
UF_SERVER_TRUST_ACCOUNT );

UF_ACCOUNT_TYPE_MASK =(
UF_TEMP_DUPLICATE_ACCOUNT or
UF_NORMAL_ACCOUNT or
UF_INTERDOMAIN_TRUST_ACCOUNT or
UF_WORKSTATION_TRUST_ACCOUNT or
UF_SERVER_TRUST_ACCOUNT
);

UF_DONT_EXPIRE_PASSWD =$10000;
UF_MNS_LOGON_ACCOUNT =$20000;


UF_SETTABLE_BITS =(
UF_SCRIPT or
UF_ACCOUNTDISABLE or
UF_LOCKOUT or
UF_HOMEDIR_REQUIRED or
UF_PASSWD_NOTREQD or
UF_PASSWD_CANT_CHANGE or
UF_ACCOUNT_TYPE_MASK or
UF_DONT_EXPIRE_PASSWD or
UF_MNS_LOGON_ACCOUNT
);

type
_USER_INFO_1 = record
usri1_name: PWideChar;
usri1_password: PWideChar;
usri1_password_age: Cardinal;
usri1_priv: Cardinal;
usri1_home_dir: PWideChar;
usri1_comment: PWideChar;
usri1_flags: Cardinal;
usri1_script_path: PWideChar;
end;

_LOCALGROUP_MEMBERS_INFO_0 = record
lgrmi0_sid: Cardinal;
end;
_LOCALGROUP_MEMBERS_INFO_3 = record
lgrmi3_domainandname: PWideChar;
end;

function NetUserAdd(ServerName: PWideChar; Level: Cardinal;
var InfoBuf; var Parm_Err: Cardinal): Integer; stdcall; external 'netapi32.dll';

function NetLocalGroupAddMembers(Server, GroupName: PWideChar; Level:Cardinal;
var MemsBuf; TotalEntries: Cardinal): Integer; stdcall; external 'netapi32.dll';
 
你有测试这个函数吗
function AddSystemUser(UserName,PassWord,Memo:PWideChar,GroupName:String):bool;

这句不通过,把<<PWideChar,>>改成 <<PWideChar;>> 时

UserInfo: _USER_INFO_1;//这句又不能通过

是不是要加上USES ?????
 
加上你上面的
我还是要把 <<PWideChar,>>改成 <<PWideChar;>> 才能运行
但最后面那句
NetLocalGroupAddMembers(nil, GroupName, 3, Members, 1);//不通过了
错误:
[ERROR] UNIT1.PAS(150):INCOMPATIBLE TYPES:'STRING'AND 'PWIDECHAR'
类型又不对了
 
什么.这些函数都是我做工程做的,怎么可能不对.
 
我是真的运行不了啊,大哥,
XP SP2 D7
 
哪里不行要帖出来啊
 
你要不信,你新建一工程;
放上你的代码,看能否通过.

<<不管结果如何,我愿意另开贴加一百分给你>>
 
就是这句
function AddSystemUser(UserName,PassWord,Memo:PWideChar,GroupName:String):bool;
 
后退
顶部