add current/selected user to a group

前端 未结 3 1861
梦毁少年i
梦毁少年i 2021-01-27 01:12

can anyone tell me how can I (programatically) add the current/selected user to a group (like power user, backup opetarors)

any function/info/code is welcomed

3条回答
  •  抹茶落季
    2021-01-27 01:23

    Here you an example using the Jedi JCL

    program Delphi_AdduserToGroup;
    
    {$APPTYPE CONSOLE}
    
    uses
      Windows,
      JclWin32,
      SysUtils;
    
    
    Procedure AddUsertoGroup(aUser,aGroup:PWideChar);
    var
      GroupMembersInfo : PLocalGroupMembersInfo3;
      ResInt           : Integer;
    begin
      GetMem(GroupMembersInfo,sizeof(TLocalGroupMembersInfo3));
      try
        //Writeln(aUser+'->'+aGroup);
        GroupMembersInfo^.lgrmi3_domainandname   :=aUser;
        ResInt:=NetLocalGroupAddMembers(nil,aGroup,3,pointer(GroupMembersInfo),1);
        case ResInt of
        NERR_Success         : Writeln('User added to group '+aGroup);
        ERROR_ACCESS_DENIED  : Writeln('The user does not have access to the requested information.');
        ERROR_NO_SUCH_MEMBER : Writeln('One or more of the members specified do not exist. Therefore, no new members were added.');
        ERROR_MEMBER_IN_ALIAS: Writeln('One or more of the members specified were already members of the local group. No new members were added.');
        ERROR_INVALID_MEMBER : Writeln('One or more of the members cannot be added because their account type is invalid. No new members were added.');
        else
         Writeln('Error '+IntToStr(ResInt));
        end;
      finally
        FreeMem(GroupMembersInfo);
      end;
    end;
    
    
    begin
      try
         AddUsertoGroup('myuser','Administrators');
         Readln;
      except
        on E:Exception do
          Writeln(E.Classname, ': ', E.Message);
      end;
    end.
    

    Bye.

提交回复
热议问题