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