-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinUtils.cpp
35 lines (26 loc) · 908 Bytes
/
WinUtils.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
35
#include "WinUtils.h"
#include <iostream>
#include <windows.h>
bool WinUtils::isRunningAsAdmin()
{
bool asAdmin = false;
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
std::cout << "WinUtils::isRunningAsAdmin(): OpenProcessToken() failed with error: " << GetLastError();
CloseHandle(hToken);
return false;
}
DWORD requiredSize;
TOKEN_ELEVATION elevation;
if (GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &requiredSize)) {
asAdmin = elevation.TokenIsElevated;
} else {
const DWORD err = GetLastError();
std::cout << "WinUtils::isRunningAsAdmin(): GetTokenInformation() failed with error: " << err << std::endl;
if (err == ERROR_INVALID_PARAMETER) {
std::cout << "WinUtils::isRunningAsAdmin(): Windows versions older than Vista are not supported." << std::endl;
}
}
CloseHandle(hToken);
return asAdmin;
}