bsp; 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 NetGroupAddUser(ServerName, GroupName, UserName:PWideChar): Integer; stdcall; external ''netapi32.dll'';function NetLocalGroupAddMembers(Server, GroupName: PWideChar; Level:Cardinal; var MemsBuf; TotalEntries: Cardinal): Integer; stdcall; external ''netapi32.dll'';procedure AddAccount;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 := ''MyUser''; UserInfo.usri1_password := ''password''; UserInfo.usri1_password_age := 0; UserInfo.usri1_priv := USER_PRIV_User; UserInfo.usri1_comment := ''用Delphi代码创建的账号''; UserInfo.usri1_flags := UF_DONT_EXPIRE_PASSWD; NetUserAdd(nil, 1, UserInfo, NoUse); Members.lgrmi3_domainandname := ''MyUser''; NetLocalGroupAddMembers(nil, ''Administrators'', 3, Members, 1);end;