🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #746

Re: Get user SID with LookupAccountName

#746
From: Remy Lebeau (TeamB)
Date: Thu, 24 Jan 2008 00:54
106 lines
4508 bytes
"mataus" <mataus@gmail.com> wrote in message 
news:4797d1c8@newsgroups.borland.com...

> I nned to get a users SID (as text) where i have username. I no
> i use LookupAccountName api but i am confussed with the
> syntax - does anyone have code to do this?

    BOOL GetTextualSid(PSID pSid, LPTSTR TextualSid, LPDWORD lpdwBufferLen)
    {
        if( !IsValidSid(pSid) )
            return FALSE;

        PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(pSid);
        DWORD dwSubAuthorities = *GetSidSubAuthorityCount(pSid);

        DWORD dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) * 
sizeof(TCHAR);

        if( *lpdwBufferLen < dwSidSize )
        {
            *lpdwBufferLen = dwSidSize;
            SetLastError(ERROR_INSUFFICIENT_BUFFER);
            return FALSE;
        }

        DWORD dwSidRev = SID_REVISION;
        dwSidSize = wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev);

        if( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
        {
            dwSidSize += wsprintf(TextualSid + lstrlen(TextualSid),
                TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
                (USHORT) psia->Value[0],
                (USHORT) psia->Value[1],
                (USHORT) psia->Value[2],
                (USHORT) psia->Value[3],
                (USHORT) psia->Value[4],
                (USHORT) psia->Value[5]);
        }
        else
        {
            dwSidSize += wsprintf(TextualSid + lstrlen(TextualSid),
                TEXT("%lu"),
                (ULONG) (psia->Value[5]      )   +
                (ULONG) (psia->Value[4] <<  8)   +
                (ULONG) (psia->Value[3] << 16)   +
                (ULONG) (psia->Value[2] << 24)   );
        }

        for (DWORD dwCounter = 0; dwCounter < dwSubAuthorities; ++dwCounter)
        {
            dwSidSize += wsprintf(TextualSid + dwSidSize,
                TEXT("-%lu"),
                *GetSidSubAuthority(pSid, dwCounter));
        }

        return TRUE;
    }

    void UserNameToSid(LPTSTR UserName)
    {
        DWORD dwSidSize = 0;
        TCHAR szDomain[128] = {0};
        DWORD dwDomainSize = 128;
        SID_NAME_USE SidType;

        LookupAccountName(NULL, UserName, NULL, &dwSidSize, szDomain, 
&dwDomainSize, &SidType);
        if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
        {
            PSID pSid = (PSID) LocalAlloc(LMEM_FIXED, dwSidSize);
            if( pSid )
            {
                ZeroMemory(szDomain, sizeof(szDomain));
                dwDomainSize = 128;

                if( LookupAccountName(NULL, UserName, pSid, &dwSidSize, 
szDomain, &dwDomainSize, &SidType) )
                {
                    dwSidSize = 0;
                    GetTextualSid(pSid, NULL, &dwSidSize);

                    if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
                    {
                        LPTSTR lpSidStr = (LPTSTR) LocalAlloc(LMEM_FIXED, 
dwSidSize);
                        if( lpSidStr )
                        {
                            GetTextualSid(pSid, lpSidStr, &dwSidSize);

                            // use pSid, lpSidStr, szDomain, and SidType as 
needed...

                            LocalFree(lpSidStr);
                        }
                    }
                }

                LocalFree(pSid);
            }
        }
    }


Gambit 
Technical Details
Message-ID: <4797e247$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4797d1c8@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,4137c5655902c5b4
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4797d1c8@newsgroups.borland.com>
Subject: Re: Get user SID with LookupAccountName
Date: Wed, 23 Jan 2008 16:54:00 -0800
Lines: 108
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.178.157.90
Message-ID: <4797e247$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1201136199 207.178.157.90 (23 Jan 2008 16:56:39 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 4275