gpt4 book ai didi

windows - 使用 Delphi 创建 Windows 用户

转载 作者:可可西里 更新时间:2023-11-01 13:50:53 24 4
gpt4 key购买 nike

我需要使用 Delphi 以管理员身份创建新的 Windows 用户

谢谢

最佳答案

您可以使用 NetUserAddNetUserSetGroups JEDI Headers 中声明的函数.

请看这个简单的示例。

program ProjectAddNewUser;

{$APPTYPE CONSOLE}

uses
JclWin32,//Jedi Library
Windows,
SysUtils;


function CreateWinUser(const wServer, wUsername, wPassword, wGroup:WideString): Boolean;
var
Buf : USER_INFO_2;//Buf for the new user info
Err : NET_API_STATUS;
ParmErr : DWORD;
GrpUsrInfo: USER_INFO_0;//Buf for the group
wDummyStr : WideString;
begin
wDummyStr:='';
FillChar (Buf, SizeOf(USER_INFO_2), 0);
with Buf do
begin
usri2_name := PWideChar(wUsername);
usri2_full_name := PWideChar(wUsername);//You can add a more descriptive name here
usri2_password := PWideChar(wPassword);
usri2_comment := PWideChar(wDummyStr);
usri2_priv := USER_PRIV_USER;
usri2_flags := UF_SCRIPT OR UF_DONT_EXPIRE_PASSWD;
usri2_script_path := PWideChar(wDummyStr);
usri2_home_dir := PWideChar(wDummyStr);
usri2_acct_expires:= TIMEQ_FOREVER;
end;

GrpUsrInfo.usri0_name:=PWideChar(wGroup);

Err := NetUserAdd(PWideChar(wServer), 1, @Buf, @ParmErr);
Result := (Err = NERR_SUCCESS);

if Result then //NOw you must set the group for the new user
begin
Err := NetUserSetGroups(PWideChar(wServer),PWideChar(wGroup),0,@GrpUsrInfo,1);
Result := (Err = NERR_SUCCESS);
end;
end;

begin

if CreateWinUser('localhost', 'MyNewUser','ThePassword','MyWindowsGroup') then
Writeln('Ok')
else
Writeln('False');

Readln;
end.

关于windows - 使用 Delphi 创建 Windows 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com