#include <windows.h>
#include <sddl.h>
#include <iostream>
std::wstring GetCurrentSID()
{
DWORD sidSize = 0;
std::wstring sidStringW = LR"()";
// Get the required buffer sizes for the SID and domain name
if (!GetUserObjectInformation(GetProcessWindowStation(), UOI_USER_SID, NULL, 0, &sidSize))
{
OutputDebugString(L"Error getting user SID buffer size\n");
return sidStringW;
}
// Allocate the necessary buffer
PSID sid = (PSID)malloc(sidSize);
// Get the user SID
if (!GetUserObjectInformation(GetProcessWindowStation(), UOI_USER_SID, sid, sidSize, NULL))
{
OutputDebugString(L"Error getting user SID\n");
return sidStringW;
}
// Convert the SID to a string
LPWSTR pSidStringW = nullptr;
if (!ConvertSidToStringSidW(sid, &pSidStringW))
{
OutputDebugString(L"Error converting SID to string\n");
return sidStringW;
}
sidStringW = pSidStringW;
if (nullptr != pSidStringW)
{
LocalFree(pSidStringW);
}
pSidStringW = nullptr;
// Free the buffer
if (nullptr != sid)
{
free(sid);
}
sid = nullptr;
return sidStringW;
}