для проверки текущего статуса высот мы можем запросить TOKEN_ELEVATION_TYPE
текущего процесса с TokenElevationType
:
демонстрационный код:
inline ULONG BTE(BOOL f)
{
return f ? 0 : GetLastError();
}
void TryElevate()
{
WCHAR path[MAX_PATH];
if (GetModuleFileNameW(0, path, RTL_NUMBER_OF(path)))
{
SHELLEXECUTEINFOW sei = { sizeof(sei), 0, 0, L"runas", path };
ShellExecuteExW(&sei);
}
}
ULONG CheckElevation()
{
HANDLE hToken;
ULONG err = BTE(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken));
if (!err)
{
ULONG cb = 0, rcb = 0x20;
union {
PTOKEN_USER ptu;
PVOID buf;
};
static volatile UCHAR guz;
PVOID stack = alloca(guz);
PWSTR SzSid = 0;
//++ for display user sid only
do
{
if (cb < rcb)
{
cb = RtlPointerToOffset(buf = alloca(rcb - cb), stack);
}
if (!(err = BTE(GetTokenInformation(hToken, ::TokenUser, buf, cb, &rcb))))
{
ConvertSidToStringSidW(ptu->User.Sid, &SzSid);
break;
}
} while (err == ERROR_INSUFFICIENT_BUFFER);
// -- for display user sid only
union {
TOKEN_ELEVATION te;
TOKEN_ELEVATION_TYPE tet;
};
if (!(err = BTE(GetTokenInformation(hToken, ::TokenElevationType, &tet, sizeof(tet), &rcb))))
{
switch (tet)
{
case TokenElevationTypeDefault:
if (!(err = BTE(GetTokenInformation(hToken, ::TokenElevation, &te, sizeof(te), &rcb))))
{
if (te.TokenIsElevated)
{
// we are built-in admin or UAC is disabled in system
case TokenElevationTypeFull:
MessageBoxW(HWND_DESKTOP, L"we run elevated", SzSid, MB_ICONINFORMATION);
break;
}
// we can not elevate under same user, but still can elevate under another admin account
// non admin account
TryElevate();
MessageBoxW(HWND_DESKTOP, L"Default", SzSid, MB_ICONINFORMATION);
}
break;
case TokenElevationTypeLimited:
// this mean that we have linked token, which is elevated. we can elevate under same user
TryElevate();
MessageBoxW(HWND_DESKTOP, L"Limited", SzSid, MB_ICONINFORMATION);
break;
default:
MessageBoxW(HWND_DESKTOP, L"unknown elevation type", SzSid, MB_ICONWARNING);
err = ERROR_GEN_FAILURE;
}
}
if (SzSid) LocalFree(SzSid);
CloseHandle(hToken);
}
return err;
}
и результат: