mpgn
mpgn

Reputation: 7251

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 get the name of the group Administrator, but the function return 0.

Any advice on this ?

#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>

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

static const DWORD MAX_BUFF_SIZE = 256;

std::wstring userNameFromSid()
{

    PSID psid;

    BOOL bSucceeded = ConvertStringSidToSid(TEXT("S-1-5-11"), &psid);
    if (bSucceeded == FALSE) {
        printf("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))
    {
        printf("group name %ws\n", buffName);
        return buffName;
    }
    printf("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 get the following error :

Error code: 87 The parameter is incorrect.

Upvotes: 1

Views: 1891

Answers (1)

ncalmbeblpaicr0011
ncalmbeblpaicr0011

Reputation: 398

LookupAccountSid() requires a PSID, not a pointer to a PSID, so &psid is incorrect.

Upvotes: 2

Related Questions