5

I need to create new windows user as administrator using Delphi

Thanks

2
  • or a command line utility that can do that thanks Commented Mar 15, 2010 at 1:21
  • solved using net user but if there is any other solution plz post thanks Commented Mar 15, 2010 at 1:50

2 Answers 2

12

you can use the NetUserAdd and NetUserSetGroups functions declarated in the JEDI Headers.

see this simple sample.

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.
Sign up to request clarification or add additional context in comments.

2 Comments

@RRUZ: i'm trying this code, but it fails on NetUserSetGroups with error 2220 (The group name could not be found). I'm trying to create a user with the "Adminitrators" group.
To make it work with the local Administrators group, I had to change three lines of code. GrpUserInfo: USER_INFO_0 became LOCALGROUP_MEMBERS_INFO_3. GrpUserInfo.usri0_name:=PWideChar(wGroup) became GrpUsrInfo.lgrmi3_domainandname := PWideChar(wUsername). And Err := NetUserSetGroups(PWideChar(wServer),PWideChar(wGroup),0,@GrpUsrInfo,1) became Err := NetLocalGroupAddMembers(PWideChar(wServer), PWideChar(wGroup), 3, @GrpUsrInfo, 1).
1

I think the API call you need is NetUserAdd.

First, check if Delphi provides a wrapper for this call. If not, you'll have to write your own. If you don't know how to make Windows API calls from Delphi, you have some more research to do.

1 Comment

+1, I couldn't find it. I've got a feeling Delphi can call these functions directly, assuming they match the correct calling convention... like you I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.