-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrivilegeCode.cpp
More file actions
45 lines (38 loc) · 871 Bytes
/
Copy pathPrivilegeCode.cpp
File metadata and controls
45 lines (38 loc) · 871 Bytes
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
#include "stdafx.h"
#include "Gr7Api.h"
// Function to change privilege of the specified process in function call
HRESULT Grass7API::Privilege::ModifyPrivilege(IN LPCTSTR szPrivilege, IN BOOL fEnable, HANDLE process)
{
HRESULT hr = S_OK;
TOKEN_PRIVILEGES NewState;
LUID luid;
HANDLE hToken = nullptr;
if (!OpenProcessToken(process,
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken))
{
return ERROR_FUNCTION_FAILED;
}
if (!LookupPrivilegeValueW(nullptr,
szPrivilege,
&luid))
{
CloseHandle(hToken);
return ERROR_FUNCTION_FAILED;
}
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes =
(fEnable ? SE_PRIVILEGE_ENABLED : 0);
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
nullptr,
nullptr))
{
hr = ERROR_FUNCTION_FAILED;
}
CloseHandle(hToken);
return hr;
}