0

Using this code in c#:

WindowsIdentity.GetCurrent().User.Value;

And getting output:

S-1-5-21-3050176684-2277112922-4090480026-1001

How to get same output in c++?

  • 2
    Does this answer your question? [How can I get the SID of the current Windows account?](https://stackoverflow.com/questions/251248/how-can-i-get-the-sid-of-the-current-windows-account) – Algirdas Preidžius Oct 04 '20 at 01:20
  • You can use `LookupAccountName`: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lookupaccountnamea?redirectedfrom=MSDN – Andy Oct 04 '20 at 01:24
  • @Andy can you show please working example, im really new to ++ – WhatAfunnyMoment Oct 04 '20 at 01:34
  • @WhatAfunnyMoment If you are new to C++, consider learning, in a structured fashion, from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Oct 04 '20 at 02:15
  • @Andy i need this code to solve one little problem only, I don't need more ++ knowledge – WhatAfunnyMoment Oct 04 '20 at 02:24
  • I wasn't suggesting you learn more C++... I posted an answer. – Andy Oct 04 '20 at 03:25

1 Answers1

0

Here is some code on how you can get the SID for an account name on the local computer using LookupAccountName (Link to API):

#include <windows.h>
#include <iostream>
#include <sddl.h>

LPCWSTR GetSid(LPCWSTR szAccountName)
{
    LPWSTR pszRet = NULL;

    DWORD dwDomainSize = 32, dwSidSize = 32;
    LPWSTR pszDomainName = new WCHAR[dwDomainSize];
    unsigned char* pSid = new unsigned char[dwSidSize];

    while (true)
    {
        SID_NAME_USE sidType;
        DWORD dwDomainSizeTmp = dwDomainSize, dwSidSizeTmp = dwSidSize;
        if (LookupAccountNameW(NULL, szAccountName, (PSID)pSid,
            &dwSidSizeTmp, pszDomainName, &dwDomainSizeTmp, &sidType))
        {
            LPWSTR pszTmp;
            if (ConvertSidToStringSidW(pSid, &pszTmp))
            {
                const int buffLen = wcslen(pszTmp) + 1;
                pszRet = new WCHAR[buffLen];
                wcscpy_s(pszRet, buffLen, pszTmp);
                LocalFree(pszTmp);
            }
            break;
        }
        else
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                if (dwDomainSizeTmp > dwDomainSize)
                {
                    delete[] pszDomainName;
                    pszDomainName = new WCHAR[dwDomainSizeTmp];
                    dwDomainSize = dwDomainSizeTmp;
                }
                if (dwSidSizeTmp > dwSidSize)
                {
                    delete[] pSid;
                    pSid = new unsigned char[dwSidSizeTmp];
                    dwSidSize = dwSidSizeTmp;
                }
            }
            else
            {
                break;
            }
        }
    }

    delete[] pszDomainName;
    delete[] pSid;

    return pszRet;
}

int main()
{
    LPCWSTR ret = GetSid(L"andy");
    if (ret)
    {
        std::wcout << L"SID for \"andy\" is: " << ret << std::endl;
        delete[] ret;
    }
    else
    {
        std::wcout << L"Failed to get SID for \"andy\"" << std::endl;
    }
    return 0;
}
Andy
  • 12,859
  • 5
  • 41
  • 56