-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetLargePagePrivilege.cpp
72 lines (62 loc) · 1.74 KB
/
SetLargePagePrivilege.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "SetLargePagePrivilege.h"
#include <Windows.h>
#include <iostream>
namespace
{
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
std::cout << "AdjustTokenPrivileges error: " << GetLastError() << std::endl;;
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
std::cout << "The token does not have the specified privilege." << std::endl;
return FALSE;
}
return TRUE;
}
}
bool LargePageTest::SetLargePagePrivilege()
{
HANDLE hToken;
auto res = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
if (!res)
{
std::cout << "Failed to get token" << std::endl;
}
res = SetPrivilege(hToken, L"SeLockMemoryPrivilege", TRUE);
if (!res)
{
std::cout << "Failed to set privilege" << std::endl;
}
return res;
}