使用Delphi创建Windows用户

时间:2010-03-15 01:13:09

标签: windows delphi

我需要使用Delphi

以管理员身份创建新的Windows用户

由于

2 个答案:

答案 0 :(得分:12)

您可以使用NetUserAdd中声明的NetUserSetGroupsJEDI 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.

答案 1 :(得分:1)

我认为您需要的API调用是NetUserAdd

首先,检查Delphi是否为此调用提供了包装器。如果没有,你将不得不自己编写。如果您不知道如何从Delphi进行Windows API调用,那么您还需要做更多的研究。