-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIsElevated.cpp
34 lines (32 loc) · 932 Bytes
/
IsElevated.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <Windows.h>
#include <cstdio>
BOOL IsElevated()
{
#if 0
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
fRet = Elevation.TokenIsElevated;
}
if (hToken)
CloseHandle(hToken);
return fRet;
#else
BOOL fRet = FALSE;
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(GetCurrentProcessToken(), TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
fRet = Elevation.TokenIsElevated;
else
fprintf(stderr, "ERROR:\tGetTokenInformation (error %d)\n", GetLastError());
return fRet;
#endif
}
int main()
{
return IsElevated() ? EXIT_SUCCESS : EXIT_FAILURE;
}