1 // Windows/MemoryLock.cpp
2
3 #include "StdAfx.h"
4
5 namespace NWindows {
6 namespace NSecurity {
7
8 #ifndef UNDER_CE
9
10 #ifdef _UNICODE
11 #define MY_FUNC_SELECT(f) :: f
12 #else
13 #define MY_FUNC_SELECT(f) my_ ## f
14 extern "C" {
15 typedef BOOL (WINAPI * Func_OpenProcessToken)(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle);
16 typedef BOOL (WINAPI * Func_LookupPrivilegeValue)(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid);
17 typedef BOOL (WINAPI * Func_AdjustTokenPrivileges)(HANDLE TokenHandle, BOOL DisableAllPrivileges,
18 PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength);
19 }
20 #define GET_PROC_ADDR(fff, name) Func_ ## fff my_ ## fff = (Func_ ## fff)GetProcAddress(hModule, name)
21 #endif
22
EnablePrivilege(LPCTSTR privilegeName,bool enable)23 bool EnablePrivilege(LPCTSTR privilegeName, bool enable)
24 {
25 bool res = false;
26
27 #ifndef _UNICODE
28
29 HMODULE hModule = ::LoadLibrary(TEXT("Advapi32.dll"));
30 if (hModule == NULL)
31 return false;
32
33 GET_PROC_ADDR(OpenProcessToken, "OpenProcessToken");
34 GET_PROC_ADDR(LookupPrivilegeValue, "LookupPrivilegeValueA");
35 GET_PROC_ADDR(AdjustTokenPrivileges, "AdjustTokenPrivileges");
36
37 if (my_OpenProcessToken &&
38 my_AdjustTokenPrivileges &&
39 my_LookupPrivilegeValue)
40
41 #endif
42
43 {
44 HANDLE token;
45 if (MY_FUNC_SELECT(OpenProcessToken)(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
46 {
47 TOKEN_PRIVILEGES tp;
48 if (MY_FUNC_SELECT(LookupPrivilegeValue)(NULL, privilegeName, &(tp.Privileges[0].Luid)))
49 {
50 tp.PrivilegeCount = 1;
51 tp.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
52 if (MY_FUNC_SELECT(AdjustTokenPrivileges)(token, FALSE, &tp, 0, NULL, NULL))
53 res = (GetLastError() == ERROR_SUCCESS);
54 }
55 ::CloseHandle(token);
56 }
57 }
58
59 #ifndef _UNICODE
60
61 ::FreeLibrary(hModule);
62
63 #endif
64
65 return res;
66 }
67
68 #endif
69
70 }}
71