MCPcopy Create free account
hub / github.com/GameTechDev/PresentMon / EnableDebugPrivilege

Function EnableDebugPrivilege

PresentMon/Privilege.cpp:27–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25}
26
27bool EnableDebugPrivilege()
28{
29 auto hmodule = LoadLibraryExA("advapi32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
30 auto pOpenProcessToken = (decltype(&OpenProcessToken)) GetProcAddress(hmodule, "OpenProcessToken");
31 auto pGetTokenInformation = (decltype(&GetTokenInformation)) GetProcAddress(hmodule, "GetTokenInformation");
32 auto pLookupPrivilegeValue = (decltype(&LookupPrivilegeValueA)) GetProcAddress(hmodule, "LookupPrivilegeValueA");
33 auto pAdjustTokenPrivileges = (decltype(&AdjustTokenPrivileges)) GetProcAddress(hmodule, "AdjustTokenPrivileges");
34 if (pOpenProcessToken == nullptr ||
35 pGetTokenInformation == nullptr ||
36 pLookupPrivilegeValue == nullptr ||
37 pAdjustTokenPrivileges == nullptr) {
38 FreeLibrary(hmodule);
39 return false;
40 }
41
42 HANDLE hToken = NULL;
43 if (pOpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) == 0) {
44 FreeLibrary(hmodule);
45 return false;
46 }
47
48 // Try to enable required privilege
49 TOKEN_PRIVILEGES tp = {};
50 tp.PrivilegeCount = 1;
51 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
52
53 if (pLookupPrivilegeValue(NULL, "SeDebugPrivilege", &tp.Privileges[0].Luid) == 0) {
54 CloseHandle(hToken);
55 FreeLibrary(hmodule);
56 return false;
57 }
58
59 auto adjustResult = pAdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr);
60 auto adjustError = GetLastError();
61
62 CloseHandle(hToken);
63 FreeLibrary(hmodule);
64
65 return
66 adjustResult != 0 &&
67 adjustError != ERROR_NOT_ALL_ASSIGNED;
68}
69
70static bool IsRestartAsAdminArg(wchar_t const* s)
71{

Callers 1

wmainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected