代码之家  ›  专栏  ›  技术社区  ›  Remus Rigo

将当前/所选用户添加到组

  •  0
  • Remus Rigo  · 技术社区  · 15 年前

    有人能告诉我如何(通过编程)将当前/选定的用户添加到一个组中(如电源用户、备份操作人员)

    欢迎使用任何功能/信息/代码

    3 回复  |  直到 15 年前
        1
  •  1
  •   tyranid    15 年前

    如果你只想在本地组中添加一个用户,那么你需要netlocalgroupaddmembers API(无论如何都要用C语言)。

    举个简单的例子:

    LOCALGROUP_MEMBERS_INFO_3 member[1];
    
    // Add using fully qualified name, could also use SID with LOCALGROUP_MEMBERS_INFO_0
    member[0].lgrmi3_domainandname = L"MAIN\\username";
    
    status = NetLocalGroupAddMembers(NULL, L"Power Users", 3, (LPBYTE)member, 1);
    

    组名只是系统上组的文本名,您可以使用如下方式通过编程确定:

    PLOCALGROUP_INFO_0 groups = NULL;
    DWORD dwCount = 0;
    DWORD dwTotalCount = 0;
    
    NET_API_STATUS status = NetLocalGroupEnum(NULL, 0, (LPBYTE*)&groups, MAX_PREFERRED_LENGTH, &dwCount, &dwTotalCount, NULL);
    
    if(status == NERR_Success)
    {
        for(DWORD i = 0; i < dwCount; i++)
        {
            printf("%ls\n", groups[i].lgrpi0_name);
        }
        NetApiBufferFree(groups);
    }
    else
    {
        printf("Error %d\n", status);
    }
    

    添加到全局组时,需要使用netgroupadduser API。

        2
  •  2
  •   RRUZ    15 年前

    这里是一个使用 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.
    

    再见。

        3
  •  0
  •   stukelly    15 年前

    你可以使用 NetLocalGroupAddMembers Windows API中的函数。

    这个 JEDI API Library 包括一个 Lan Manager Access API interface Unit ,用于Delphi。