-
Notifications
You must be signed in to change notification settings - Fork 0
/
wincommon.c
52 lines (45 loc) · 1.26 KB
/
wincommon.c
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
#include <Windows.h>
#include <stdio.h>
void PrintError(DWORD errorCode) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
printf("Error code was: %ld\n", errorCode);
printf("Error message was: %ws", lpMsgBuf);
LocalFree(lpMsgBuf);
}
void ObtainPrivileges(LPCTSTR privilege) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL res;
DWORD error;
// Obtain required privileges
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
printf("OpenProcessToken failed!\n");
PrintError(GetLastError());
exit(GetLastError());
}
res = LookupPrivilegeValue(NULL, privilege, &tkp.Privileges[0].Luid);
if (!res) {
printf("LookupPrivilegeValue failed!\n");
PrintError(GetLastError());
exit(GetLastError());
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
error = GetLastError();
if (error != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges failed\n");
PrintError(error);
exit(error);
}
}