Get Windows local group name by SID
NickName:Aria Fathi Ask DateTime:2019-07-09T16:59:22

Get Windows local group name by SID

I want to get the name of a local group from SID and then put it in a variable and then add a user to that group.

I've already tried this but I don't get any output, expecting the group name:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <stdio.h>
#include <stdlib.h>
#include <Sddl.h>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <iostream>

using namespace std;

#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "Advapi32.lib")

static const DWORD MAX_BUFF_SIZE = 256;

std::wstring userNameFromSid()
{

    PSID psid = NULL;

    BOOL bSucceeded = ConvertStringSidToSid(TEXT("S-1-5-32-544"), &psid);
    if (bSucceeded == FALSE) {
        cout<<("Error Converting SID to String");
    }

    wchar_t buffName[MAX_BUFF_SIZE];
    DWORD buffNameSize = MAX_BUFF_SIZE;
    wchar_t buffDomain[MAX_BUFF_SIZE];
    DWORD buffDomainSize = MAX_BUFF_SIZE;
    SID_NAME_USE SidType = SidTypeGroup;

    if (LookupAccountSid(NULL, psid, buffName, &buffNameSize, NULL, &buffDomainSize, &SidType))
    {
        cout<<("group name %ws\n", buffName);
        getch();
        return buffName;
    }
    cout<<("Error code: %d", GetLastError());


    //LocalFree(psid);

    /*Here some code to print error in a Message box*/
    return L"";
}
int main()
{
    NET_API_STATUS err = 0;
    userNameFromSid();
    return(0);
}

I'm not sure if I can add user to a group with SID (without using Group name like Administrators), but if that's possible, then it will do the job.

Copyright Notice:Content Author:「Aria Fathi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56948892/get-windows-local-group-name-by-sid

More about “Get Windows local group name by SID” related questions

Get Windows local group name by SID

I want to get the name of a local group from SID and then put it in a variable and then add a user to that group. I've already tried this but I don't get any output, expecting the group name: #if...

Show Detail

Get Non Pre-Windows 2000 Group Name from SID

I have a SID which represents a group on a different domain. I am trying to get the group name using: $objSid = New-Object 'System.Security.Principal.SecurityIdentifier'($Sid) $objUser = $objSid.

Show Detail

Translate SID to name

My Delphi 2010 application needs to add a Windows user to the local Administrators group. I got this part working using NetLocalGroupAddMembers. Now the application needs to work in localized ver...

Show Detail

Get local user by SID

Question: I want to get a local windows user by SID: I found this code [ADSI]("WinNT://$Env:Computername/&lt;SID=S-1-5-18&gt;") here: http://www.eggheadcafe.com/software/aspnet/32882679/add-buil...

Show Detail

Get group name from SID

I want to use LookupGroupId function in os/user with this definition : func LookupGroupId(gid string) (*Group, error) { return lookupGroupId(gid) } and here is the lookupGroupId definition: func

Show Detail

Get user group memberships from SID

I'm querying AD groups outside our local domain. When searching for the groups in PS I've got all the members that are displayed with their SID and not with their User ID. What I basically want is to

Show Detail

how to get domain user SID which is part of local system group

I have a machine within a domain (no work-group) and I have a local group Test Users and within this group I have added domain users. domain\Administrator domain\Install Now I want to fetch all ...

Show Detail

How do I specify the account SID when creating a local group account?

Is is possible to specify the SID when creating a new local group account? I've successfully created a new group using the NetLocalGroupAdd function by specifying information level 1 (LOCALGROUP_I...

Show Detail

Get name of LocalGroup Windows C++ using SID

I'm trying to get the name of a group regarding the SID of the group. The SID of the local admin group is S-1-5-32-544 for example. I use the function ConvertStringSidToSid and LookupAccountSid to ...

Show Detail

Sid of local group in machine

I try to find sid of the administrator group for example. According to Microsoft the sid of that group is: S-1-5-21-machine-500 when the machine is identifier represents the three sub-authority val...

Show Detail