add current/selected user to a group

前端 未结 3 1899
梦毁少年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:38

    Well if all you want to do is add a user to a local group then you want the NetLocalGroupAddMembers API (to do it in C anyway).

    As a simple example:

    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);
    

    The group name is just the textual name of the group on the system which you can determine programatically using something like:

    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);
    }
    

    Adding to global group you will need to instead use the NetGroupAddUser API.

提交回复
热议问题